void timer_checkIdle(object sender, EventArgs e) // verifica inattività superiore ai 5 minuti { if (IdleDetection.GetIdleTime() >= 300000) { StopAllTimers(); MessageBoxResult idle = System.Windows.MessageBox.Show("A causa di un'inattività prolungata, il timer del progetto è stato arrestato. Clicca nuovamente su un progetto per ricominciare a tracciare il tempo di lavoro.", "InTime", MessageBoxButton.OK); this.Show(); } }
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"); } }