private static void Run()
        {
            try {
                using (var service = new ServiceController(AppService.Instance.ServiceName)) {
                    bool?lastIsRunning = null;
                    Tray.SetStatusToUnknown();

                    while (!CancelEvent.WaitOne(250, false))
                    {
                        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;
                    }
                }
            } catch (ThreadAbortException) { }
        }
Example #2
0
        internal static void Main(string[] args)
        {
            var parameter = string.Join(" ", args).TrimStart(new char[] { '/', '-' });

            if (parameter.Equals("interactive", StringComparison.InvariantCultureIgnoreCase))    // just for testing - no service necessary

            {
                Tray.Show();
                ServiceWorker.Start();
                Tray.SetStatusToRunningInteractive();
                Application.Run();
                ServiceWorker.Stop();
                Tray.Hide();
                Environment.Exit(0);
            }
            else if (parameter.Equals("install", StringComparison.InvariantCultureIgnoreCase))      // install service

            {
                try {
                    using (var sc = new ServiceController(AppService.Instance.ServiceName)) {
                        if (sc.Status != ServiceControllerStatus.Stopped)
                        {
                            sc.Stop();
                        }
                    }
                } catch (Exception) { }

                ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
                Environment.Exit(0);
            }
            else if (parameter.Equals("uninstall", StringComparison.InvariantCultureIgnoreCase))      // uninstall service

            {
                try {
                    using (var sc = new ServiceController(AppService.Instance.ServiceName)) {
                        if (sc.Status != ServiceControllerStatus.Stopped)
                        {
                            sc.Stop();
                        }
                    }
                } catch (Exception) { }

                try {
                    ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                    Environment.Exit(0);
                } catch (InstallException) {  // no service with that name
                    Environment.Exit(1);
                }
            }
            else if (parameter.Equals("start", StringComparison.InvariantCultureIgnoreCase))      // start service

            {
                try {
                    using (var service = new ServiceController("BacklightShifter")) {
                        if (service.Status != ServiceControllerStatus.Running)
                        {
                            service.Start();
                            service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 1));
                        }
                    }
                } catch (Exception) { }
                Environment.Exit(0);
            }
            else if (Environment.UserInteractive)      // run tray status

            {
                Tray.Show();
                ServiceStatusThread.Start();
                Application.Run();
                ServiceStatusThread.Stop();
                Tray.Hide();
                Environment.Exit(0);
            }
            else      // you're running as a service

            {
                ServiceBase.Run(new ServiceBase[] { AppService.Instance });
            }
        }