Example #1
0
        public void Start()
        {
            //if (!Environment.UserInteractive)
            //{
            //	RequestAdditionalTime(120000);
            //}
            LogToFile.Info("Start method called");
            myServiceStatus.currentState = (int)State.SERVICE_START_PENDING;
            SetServiceStatus(ServiceHandle, ref myServiceStatus);
            LogToFile.Info("Service status set to pending");

            //if (String.IsNullOrEmpty(Settings.Default.SqlDatabaseName))
            //{
            //	LogEvent.InfoLimited("Backup database name is not set in config file, " +
            //						 "unable to continue", new TimeSpan(1, 0, 0));
            //}
            //else
            if (workerThread == null ||
                threadStatus == ThreadStatus.None ||
                threadStatus == ThreadStatus.Stopped)
            {
                // Start a separate thread that does the actual work.
                try
                {
                    LogToFile.Info("Creating worker thread");
                    workerThread = new Thread(ServiceWorkerMethod);
                    workerThread.Start();
                    LogToFile.Info("Started worker thread");
                }
                catch (Exception ex)
                {
                    LogEvent.Exception(ex, ESeverityLevel.SeverityError);
                }
            }
            else
            {
                LogEvent.Info("Unable to start thread, due to following:" + Environment.NewLine +
                              "\tWorker thread NULL: " + (workerThread == null ? "True" : "False") + Environment.NewLine +
                              "\tThread status: " + threadStatus);
            }
            if (workerThread != null)
            {
                LogEvent.Info("Start - Worker thread state: {0}\r\nThread status: {1}",
                              workerThread.ThreadState.ToString(), threadStatus.ToString());
            }
            myServiceStatus.currentState = (int)State.SERVICE_RUNNING;
            SetServiceStatus(ServiceHandle, ref myServiceStatus);
        }
Example #2
0
        public void ServiceWorkerMethod()
        {
            try
            {
                LogToFile.Info("Service worker running");
                bool endThread = false;

                threadStatus = ThreadStatus.Running;
                int sleepSeconds;
                int secondsToSleep = Int32.TryParse(Resources.WorkerThreadIntervalSeconds, out sleepSeconds)
                                                                                 ? sleepSeconds
                                                                                 : 60;

                LogToFile.Info("IdleService was started successfully", ESeverityLevel.SeverityInfo);
                ProcessState = ServiceState.IdleWait;
                WaitUntil    = DateTime.MinValue;
                int idleMinimumSeconds = Settings.Default.IdleBeforeShutdown * 60;

                while (!endThread)
                {
                    if (pauseEvent.WaitOne(0))
                    {
                        threadStatus = ThreadStatus.Paused;
                        LogEvent.Info("Pause signal received at " + DateTime.Now);
                    }
                    else if (threadStatus != ThreadStatus.Paused)
                    {
                        //if (somethingHasFailed)
                        //{
                        //	endThread = true;
                        //}

                        switch (ProcessState)
                        {
                        case ServiceState.IdleWait:
                            if (WaitUntil != DateTime.MinValue && DateTime.Now < WaitUntil)
                            {
                                break;
                            }
                            uint secondsIdle = IdleDetection.GetLastInputTime();
                            if (secondsIdle > idleMinimumSeconds)
                            {
                                LogToFile.Info("Exceeded idle time limit of " + idleMinimumSeconds + Environment.NewLine +
                                               "Calling shutdown to halt the computer");
                                string systemFolder = Environment.GetFolderPath(Environment.SpecialFolder.System);
                                string shutdownExe  = Path.Combine(systemFolder, "shutdown.exe");

                                if (File.Exists(shutdownExe))
                                {
                                    LogEvent.Info("Executing shutdown after {0} seconds of inactivity.",
                                                  secondsIdle);

                                    ProcessLauncher.ExecuteCommandLine(shutdownExe, @"/s /t 30", systemFolder);
                                    WaitUntil    = DateTime.Now.AddSeconds(60);                                          //TODO: Convert to constant
                                    ProcessState = ServiceState.ShutdownWait;
                                }
                                else
                                {
                                    WaitUntil = DateTime.Now.AddMinutes(60);
                                    LogEvent.Error("Failed to execute shutdown after {0} seconds of inactivity." +
                                                   Environment.NewLine + "Shutdown not found at: {1}",
                                                   secondsIdle, shutdownExe);
                                }
                            }
                            break;

                        case ServiceState.ShutdownWait:
                            if (WaitUntil != DateTime.MinValue && DateTime.Now < WaitUntil)
                            {
                                break;
                            }
                            LogToFile.Info("Killing all processes matching the logged on user (except shutdown itself)");
                            ProcessLauncher.KillProcessesMatchingLoggedOnUser();
                            WaitUntil    = DateTime.Now.AddSeconds(60);                                  //TODO: Convert to constant
                            ProcessState = ServiceState.KillUserProcess;
                            break;

                        case ServiceState.KillUserProcess:
                            if (WaitUntil != DateTime.MinValue && DateTime.Now < WaitUntil)
                            {
                                break;
                            }
                            // Nothing left to do but start the process over
                            LogToFile.Info("Killing user processes did not seem to work, will retry shutdown in 5 minutes");
                            WaitUntil    = DateTime.Now.AddMinutes(5);
                            ProcessState = ServiceState.IdleWait;
                            break;
                        }
                    }
                    else if (continueThread.WaitOne(0))
                    {
                        threadStatus = ThreadStatus.Running;
                    }

                    for (int i = 0; i < secondsToSleep && !endThread; i++)
                    {
                        Thread.Sleep(1000);
                        if (stopEvent.WaitOne(0))
                        {
                            endThread = true;
                            LogEvent.Info("Stop event signaled at " + DateTime.Now.ToString());
                        }
                    }
                }
            }
            catch (ThreadAbortException)
            {
                LogEvent.Info("Worker thread has been aborted, shutting down");
            }
            catch (Exception ex)
            {
                LogEvent.Error("Exception while running main service worker thread:\r\n\r\n" +
                               ex);
            }
            finally
            {
                threadStatus = ThreadStatus.Stopped;
                LogEvent.Info("Main service worker thread exiting");
            }
        }
Example #3
0
 protected override void OnStart(string[] args)
 {
     LogToFile.Info("Service start requested, ending the service");
     Start();
 }
Example #4
0
 protected override void OnStop()
 {
     LogToFile.Info("Service stop requested, ending the service");
     DoStop();
 }