宝剑锋从磨砺出,梅花香自苦寒来。这篇文章主要讲述Application.idle方法相关的知识,希望能为你提供帮助。
Application.Idle()方法表示:当应用程序处于空闲状态时执行相应代码。
【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(); }/// < summary> /// 初始化Timer控件 /// < /summary> 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(); }/// < summary> /// 定时到点执行的事件 /// < /summary> /// < param name="sender"> < /param> /// < param name="e"> < /param> 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); }/// < summary> /// 程序进入空闲时期时会一直执行此事件 /// < /summary> /// < param name="sender"> < /param> /// < param name="e"> < /param> private void OnApplicationIdle(object sender, EventArgs e) { if (timer != null) timer.Start(); }/// < summary> /// 当键盘及鼠标事件,关闭timer /// < /summary> /// < param name="sender"> < /param> /// < param name="e"> < /param> 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; /// < summary> /// 只要是按键盘及鼠标便会引发事件 /// 因为是为了监视键盘及鼠标,所以均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 /// < /summary> 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; } } }
推荐阅读
- Android 自定义控件之继承ViewGroup创建新容器
- android的控件
- Android binder学习一(主要概念)
- uiautomator android 官网翻译
- Android 软键盘弹出时布局内指定内容上移实现及问题解决
- NiceFish的ERROR in AppModule is not an NgModule问题
- 如何设置kubectl端口转发(详细分步指南)
- 如何在Kubernetes集群上安装Jenkins(详细分步指南)
- 如何在Kubernetes上部署RabbitMQ(分步指南)