internal static void Main() { System.AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; if (Medo.Application.Args.Current.ContainsKey("Interactive")) { Tray.Show(); ServiceThread.Start(); Tray.SetStatusToRunningInteractive(); Application.Run(); ServiceThread.Stop(); Tray.Hide(); Environment.Exit(0); } else if (Medo.Application.Args.Current.ContainsKey("Install")) { ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location }); System.Environment.Exit(0); } else if (Medo.Application.Args.Current.ContainsKey("Uninstall")) { try { ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location }); System.Environment.Exit(0); } catch (System.Configuration.Install.InstallException) { //no service with that name System.Environment.Exit(-1); } } else { if (Environment.UserInteractive) { Tray.Show(); ServiceStatusThread.Start(); Application.Run(); ServiceStatusThread.Stop(); Tray.Hide(); Environment.Exit(0); } else { ServiceBase.Run(new ServiceBase[] { AppService.Instance }); } } }
private static void Run() { try { var sw = new Stopwatch(); using (var service = new ServiceController(AppService.Instance.ServiceName)) { bool?lastIsRunning = null; Tray.SetStatusToUnknown(); while (!ServiceStatusThread.CancelEvent.WaitOne(0, false)) { if ((sw.IsRunning == false) || (sw.ElapsedMilliseconds > 1000)) { bool?currIsRunning; try { service.Refresh(); currIsRunning = (service.Status == ServiceControllerStatus.Running); } catch (InvalidOperationException) { currIsRunning = null; } if (lastIsRunning != currIsRunning) { if (currIsRunning == null) { Tray.SetStatusToUnknown(); } else if (currIsRunning == true) { Tray.SetStatusToRunning(); } else { Tray.SetStatusToStopped(); } } lastIsRunning = currIsRunning; sw.Reset(); sw.Start(); } Thread.Sleep(100); } } } catch (ThreadAbortException) { } }