網站首頁 編程語言 正文
Application.Idle()方法表示:當應用程序處于空閑狀態時執行相應代碼。
示例程序
1、界面設計:一個簡單的Lable控件
2、代碼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; namespace ApplicationIdleDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public System.Timers.Timer timer; private void Form1_Load(object sender, EventArgs e) { InitTimer(); InitRefresh(); Refresh(); } ////// 初始化Timer控件 /// private void InitTimer() { timer = new System.Timers.Timer(120000); //到達定時時間的時候執行的事件 timer.Elapsed += new System.Timers.ElapsedEventHandler(TimeUp); //設置是執行一次(false) 還是一直執行(true) timer.AutoReset = true; //是否執行System.Timers.Timer.Elapsed事件 timer.Enabled = true; //啟動 timer.Start(); } ////// 定時到點執行的事件 /// /// /// public void TimeUp(object sender, System.Timers.ElapsedEventArgs e) { Refresh(); } private void Refresh() { this.lbl_idle.Text = "進入空閑期"; string strPath = Application.StartupPath + @"test.txt"; using (StreamWriter sw = new StreamWriter(strPath, true)) { sw.WriteLine("開始進入空閑期,當前時間:" + DateTime.Now); sw.Close(); } } private void InitRefresh() { //設定IDLE自動結束 Application.Idle += new EventHandler(OnApplicationIdle); //設定消息過濾 FormMessageFilter MessageFilter = new FormMessageFilter(); MessageFilter.ApplicationActive += new EventHandler(OnApplicationActive); Application.AddMessageFilter(MessageFilter); } ////// 程序進入空閑時期時會一直執行此事件 /// /// /// private void OnApplicationIdle(object sender, EventArgs e) { if (timer != null) timer.Start(); } ////// 當鍵盤及鼠標事件,關閉timer /// /// /// private void OnApplicationActive(object sender, EventArgs e) { if (timer != null) { timer.Stop(); EndIdle(); } } private void EndIdle() { this.lbl_idle.Text = "結束空閑期,進入活動期"; string strPath = Application.StartupPath + @"test.txt"; using (StreamWriter sw = new StreamWriter(strPath,true)) { sw.WriteLine("開始進入活動期,當前時間:" + DateTime.Now); sw.Close(); } } } public class FormMessageFilter : IMessageFilter { public event EventHandler ApplicationActive; ////// 只要是按鍵盤及鼠標便會引發事件 /// 因為是為了監視鍵盤及鼠標,所以均return false; /// return ture:會把輸入的值清除 /// 0x100 /* WM_KEYDOWN /// 0x101 /* WM_KEYUP /// 0x200 /* WM_MOUSEMOVE /// 0x201 /* WM_LBUTTONDOWN /// 0x202 /* WM_LBUTTONUP /// 0x203 /* WM_LBUTTONDBLCLK /// 0x204 /* WM_RBUTTONDOWN /// 0x205 /* WM_RBUTTONUP /// 0x206 /* WM_RBUTTONDBLCLK /// 0x20a /* WM_MOUSEWHEEL /// public bool PreFilterMessage(ref Message m) { if (m.Msg == 0x100 || m.Msg == 0x101 || (m.Msg > 0x199 && m.Msg < 0x207) || m.Msg == 0x20a) { if (ApplicationActive != null) { ApplicationActive(this, new EventArgs()); } } return false; } } }
原文鏈接:https://www.cnblogs.com/dotnet261010/p/6837870.html
相關推薦
- 2023-07-09 .Net下驗證MongoDB 的 Linq 模式聯合查詢是否可用
- 2022-09-21 jquery實現網頁左側導航菜單欄_jquery
- 2022-11-17 VMware?vSphere?ESXi系統設置靜態IP的方法_VMware
- 2022-02-23 Linux用戶建立腳本/猜字游戲/網卡流量監控介紹_Linux
- 2022-05-13 C++ std::thread 線程的傳參方式
- 2024-03-19 Linux中 find 命令詳解
- 2022-07-26 注冊bean有多少種方式
- 2022-09-22 使用einops簡化數據維度操作
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支