/// <summary> /// 活跃状态进行空闲检测,非活跃状态进行繁忙检测 /// </summary> /// <param name="source"></param> /// <param name="e"></param> public void IdleMonitor(Object source, ElapsedEventArgs e) { // 无操作毫秒数 uint interval = InputWatcher.GetLastInputTimeInterval(ref IdleWatcher.LastInputInfo); System.Timers.Timer timer = (System.Timers.Timer)source; if (!isActive) { Console.WriteLine("Idle status"); // 非活跃状态下 // 判断无操作时间是否小于繁忙检测时间间隔 // 如小于则认定系统处于活跃状态 isActive = interval < BusyDetectionTimeInterval ? true : true; if (isActive) { Process process = IWindowManager.GetForegroundWindowProcess(); if (process != null && AppWindows.ContainsKey(process.MainModule.FileName)) { AppWindows[process.MainModule.FileName].SetForeground(); } timer.Interval = IdleDetectionTimeInterval; } } else { Console.WriteLine("Busy status"); // 活跃状态下 // 判断无操作时间是否小于空闲检测时间间隔 // 如小于则认定系统处于活跃状态 isActive = interval < IdleDetectionTimeInterval ? true : false; if (isActive) { timer.Interval = IdleDetectionTimeInterval - interval; } else { Process process = IWindowManager.GetForegroundWindowProcess(); if (process != null && AppWindows.ContainsKey(process.MainModule.FileName)) { AppWindows[process.MainModule.FileName].Update(); } timer.Interval = BusyDetectionTimeInterval; } } }
private void MainForm_Load(object sender, EventArgs e) { // 监控窗口切换 WindowForegroundWatcher = new WindowWatcher(); WindowForegroundWatcher.windowSwitchEvent += WindowSwitch; WindowForegroundWatcher.Init(); // 监控窗口从最小化恢复 WindowMinimizeWatcher = new WindowWatcher((int)DLLInvoke.EVENT_SYSTEM_MINIMIZEEND, (int)DLLInvoke.EVENT_SYSTEM_MINIMIZEEND, 0, 0); WindowMinimizeWatcher.windowSwitchEvent += WindowSwitch; WindowMinimizeWatcher.Init(); // 系统输入监听 IdleWatcher = new InputWatcher(IdleDetectionTimeInterval); IdleWatcher.Timer.Elapsed += IdleMonitor; IdleWatcher.Start(); // 设置排序方法 ListViewSort listViewSort = new ListViewSort(ListViewSort.DESCENDING); listViewSort.CompareMethod += GetCompareValue; AppList.ListViewItemSorter = listViewSort; }