網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Application.Idle()方法表示:當(dāng)應(yīng)用程序處于空閑狀態(tài)時(shí)執(zhí)行相應(yīng)代碼。
示例程序
1、界面設(shè)計(jì):一個(gè)簡(jiǎn)單的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); //到達(dá)定時(shí)時(shí)間的時(shí)候執(zhí)行的事件 timer.Elapsed += new System.Timers.ElapsedEventHandler(TimeUp); //設(shè)置是執(zhí)行一次(false) 還是一直執(zhí)行(true) timer.AutoReset = true; //是否執(zhí)行System.Timers.Timer.Elapsed事件 timer.Enabled = true; //啟動(dòng) timer.Start(); } ////// 定時(shí)到點(diǎn)執(zhí)行的事件 /// /// /// public void TimeUp(object sender, System.Timers.ElapsedEventArgs e) { Refresh(); } private void Refresh() { this.lbl_idle.Text = "進(jìn)入空閑期"; string strPath = Application.StartupPath + @"test.txt"; using (StreamWriter sw = new StreamWriter(strPath, true)) { sw.WriteLine("開始進(jìn)入空閑期,當(dāng)前時(shí)間:" + DateTime.Now); sw.Close(); } } private void InitRefresh() { //設(shè)定IDLE自動(dòng)結(jié)束 Application.Idle += new EventHandler(OnApplicationIdle); //設(shè)定消息過濾 FormMessageFilter MessageFilter = new FormMessageFilter(); MessageFilter.ApplicationActive += new EventHandler(OnApplicationActive); Application.AddMessageFilter(MessageFilter); } ////// 程序進(jìn)入空閑時(shí)期時(shí)會(huì)一直執(zhí)行此事件 /// /// /// private void OnApplicationIdle(object sender, EventArgs e) { if (timer != null) timer.Start(); } ////// 當(dāng)鍵盤及鼠標(biāo)事件,關(guān)閉timer /// /// /// private void OnApplicationActive(object sender, EventArgs e) { if (timer != null) { timer.Stop(); EndIdle(); } } private void EndIdle() { this.lbl_idle.Text = "結(jié)束空閑期,進(jìn)入活動(dòng)期"; string strPath = Application.StartupPath + @"test.txt"; using (StreamWriter sw = new StreamWriter(strPath,true)) { sw.WriteLine("開始進(jìn)入活動(dòng)期,當(dāng)前時(shí)間:" + DateTime.Now); sw.Close(); } } } public class FormMessageFilter : IMessageFilter { public event EventHandler ApplicationActive; ////// 只要是按鍵盤及鼠標(biāo)便會(huì)引發(fā)事件 /// 因?yàn)槭菫榱吮O(jiān)視鍵盤及鼠標(biāo),所以均return false; /// return ture:會(huì)把輸入的值清除 /// 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
相關(guān)推薦
- 2022-05-04 C#設(shè)計(jì)模式之策略模式_C#教程
- 2022-10-09 淺談重繪和回流的解析_基礎(chǔ)教程
- 2023-03-13 Go語(yǔ)言中GORM存取數(shù)組/自定義類型數(shù)據(jù)_Golang
- 2023-04-06 C++中的多態(tài)問題—理解虛函數(shù)表及多態(tài)實(shí)現(xiàn)原理_C 語(yǔ)言
- 2022-06-23 C++詳細(xì)講解常用math函數(shù)的用法_C 語(yǔ)言
- 2022-05-17 C++?std::shared_mutex讀寫鎖的使用_C 語(yǔ)言
- 2022-04-18 設(shè)置彈性布局的三列兩側(cè)對(duì)齊,最后一行樣式的處理
- 2023-11-12 ip link set eno2 down后centos無法聯(lián)網(wǎng);centos7.0,二次啟動(dòng)后無法
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支