Пример #1
0
 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) { }
 }
Пример #2
0
        static void Main()
        {
            System.AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            if (Medo.Application.Args.Current.ContainsKey("Interactive"))
            {
                Tray.Show();
                Service.Start();
                Tray.SetStatusToRunningInteractive();
                Application.Run();
                Service.Stop();
                Tray.Hide();
                Environment.Exit(0);
            }
            else if (Medo.Application.Args.Current.ContainsKey("Install"))
            {
                try {
                    using (ServiceController sc = new ServiceController(AppService.Instance.ServiceName)) {
                        if (sc.Status != ServiceControllerStatus.Stopped)
                        {
                            sc.Stop();
                        }
                    }
                } catch (Exception) { }

                ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
                System.Environment.Exit(0);
            }
            else if (Medo.Application.Args.Current.ContainsKey("Uninstall"))
            {
                try {
                    using (ServiceController sc = new ServiceController(AppService.Instance.ServiceName)) {
                        if (sc.Status != ServiceControllerStatus.Stopped)
                        {
                            sc.Stop();
                        }
                    }
                } catch (Exception) { }
                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 (Medo.Application.Args.Current.ContainsKey("Start"))
            {
                try {
                    using (var service = new ServiceController("VhdAttach")) {
                        if (service.Status != ServiceControllerStatus.Running)
                        {
                            service.Start();
                            service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 1));
                        }
                    }
                } catch (Exception) { }
                System.Environment.Exit(0);
            }
            else
            {
                if (Environment.UserInteractive)
                {
                    Tray.Show();
                    ServiceStatusThread.Start();
                    Application.Run();
                    ServiceStatusThread.Stop();
                    Tray.Hide();
                    Environment.Exit(0);
                }
                else
                {
                    ServiceBase.Run(new ServiceBase[] { AppService.Instance });
                }
            }
        }