Exemple #1
0
        private void LoadData(bool _bReload)
        {
            try
            {
                if (((m_dictData != null) && (m_bIsDataLoaded == true)) &&
                    (_bReload == false))
                {
                    return;
                }

                string sDataFilePath = GetFilePath();

                if (File.Exists(sDataFilePath))
                {
                    string sData = Compression.Decompress(File.ReadAllText(sDataFilePath));

                    m_dictData = (Dictionary <K, T>)JsonConvert.DeserializeObject(sData, typeof(Dictionary <K, T>));

                    m_bIsDataLoaded = true;
                }
                else
                {
                    Logger.WriteErrorLogOnly("The data file {0} not found.", "8dcf0421-d278-4220-885a-cde09bd3f531");
                }
            }
            catch (Exception exp)
            {
                Logger.WriteError(exp, "089c9cf5-d69f-42d9-8d10-688009765d9e");

                throw new Exception("Failed to read configuration file.");
            }
        }
Exemple #2
0
        public static void Main_WinTrayApp(CustomApplicationContext _CustomApplicationContext, string _sApplicationName, bool _bShouldRunAsSingleInstance)
        {
            try
            {
                bool createdNew = true;

                Process currentProcess = Process.GetCurrentProcess();
                string  sAppName       = (string.IsNullOrWhiteSpace(_sApplicationName) == true) ? currentProcess.ProcessName : _sApplicationName.RemoveWhiteSpace();

                using (Mutex mutex = new Mutex(true, sAppName, out createdNew))
                {
                    // SKislyuk 11/3/2016 2:23:09 PM
                    // if the app can run as multiple instances app then disregard the mutex
                    createdNew = (_bShouldRunAsSingleInstance == true) ? createdNew : true;

                    if (createdNew)
                    {
                        if (_CustomApplicationContext == null)
                        {
                            throw new Exception("The CustomApplicationContext class is not initialized.");
                        }

                        Application.Run(_CustomApplicationContext);
                    }
                    else
                    {
                        ShowProcess();
                    }
                }
            }
            catch (Exception exp)
            {
                Logger.WriteErrorLogOnly(exp, "b4d12487-4e99-4ed9-a9f4-38e02de2f41f");
                Logger.DisplayError(exp.Message);
            }
        }
Exemple #3
0
        public static void Main_Service_Console <T>(StartDelegate _StartDelegate, StopDelegate _StopDelegate, string _sApplicationName, bool _bShouldRunAsSingleInstance)
        {
            if (Environment.UserInteractive)
            {
                try
                {
                    bool createdNew = true;

                    Process currentProcess = Process.GetCurrentProcess();
                    string  sAppName       = (string.IsNullOrWhiteSpace(_sApplicationName) == true) ? currentProcess.ProcessName : _sApplicationName.RemoveWhiteSpace();

                    using (Mutex mutex = new Mutex(true, sAppName, out createdNew))
                    {
                        // SKislyuk 11/3/2016 2:23:09 PM
                        // if the app can run as multiple instances app then disregard the mutex
                        createdNew = (_bShouldRunAsSingleInstance == true) ? createdNew : true;

                        if (createdNew)
                        {
                            //@cmnt SKislyuk: [03 November 16, 14:33:02]   [161103_143302]
                            // running as console app
                            //
                            //if (_StartDelegate != null)
                            //{
                            //	_StartDelegate();
                            //}
                            // the following like equivalent to code commented above
                            _StartDelegate?.Invoke();


                            Console.WriteLine("Press any key to stop...");
                            Console.ReadKey(true);
                            // SKislyuk 5/4/2018 11:42:43 AM
                            // stopping app
                            //
                            //if (_StopDelegate != null)
                            //{
                            //_StopDelegate();
                            //}
                            // the following like equivalent to code commented above
                            _StopDelegate?.Invoke();
                        }
                        else
                        {
                            ShowProcess();
                        }
                    }
                }
                catch (Exception exp)
                {
                    Logger.WriteErrorLogOnly(exp, "cc27108e-0987-4cbf-b9de-69001127b72f");
                }
            }
            else
            {
                try
                {
                    // running as service
                    using (ServiceBase service = (ServiceBase)Activator.CreateInstance(typeof(T)))
                    {
                        ServiceBase.Run(service);
                    }
                }
                catch (Exception exp)
                {
                    Logger.WriteErrorLogOnly(exp, "ed78d049-1bc4-47f9-8f21-8c25376714fe");
                }
            }
        }