/// <summary> /// Подпрограмма запуска платы и перехода в рабочий режим /// </summary> /// <param name="mode">Командное слово режима. Значение по умолчанию 0</param> /// <returns></returns> public int Start(ushort mode = 0) { // Synchronously wait to enter the Semaphore. SemaphoreSlim.Wait(); var error = _start?.Invoke(ref _device, mode) ?? int.MaxValue; // Release the Semaphore. SemaphoreSlim.Release(); return(error); }
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"); } } }