The Application.idle () method indicates that the appropriate code is executed when the application is in an idle state.
Sample Program
1. Interface design: A simple lable control
2. Code
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.IO;namespaceapplicationidledemo{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } PublicSystem.Timers.Timer Timer; Private voidForm1_Load (Objectsender, EventArgs e) {Inittimer (); Initrefresh (); Refresh (); } /// <summary> ///Initializing the Timer control/// </summary> Private voidInittimer () {Timer=NewSystem.Timers.Timer (120000); //events that are executed when the time is reachedTimer. Elapsed + =NewSystem.Timers.ElapsedEventHandler (timeup); //Whether the setting is executed once (false) or always executed (true)Timer. AutoReset =true; //whether to perform the System.Timers.Timer.Elapsed eventTimer. Enabled =true; //Starttimer. Start (); } /// <summary> ///event timed to point execution/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Public voidTimeup (Objectsender, System.Timers.ElapsedEventArgs e) {Refresh (); } Private voidRefresh () { This. Lbl_idle. Text ="Enter the idle period"; stringstrpath = Application.startuppath +@"Test.txt"; using(StreamWriter SW =NewStreamWriter (strpath,true) ) {SW. WriteLine ("start entering idle period, current time:"+DateTime.Now); Sw. Close (); } } Private voidInitrefresh () {//set Idle to end automaticallyApplication.idle + =NewEventHandler (Onapplicationidle); //Set Message FilteringFormmessagefilter MessageFilter =NewFormmessagefilter (); Messagefilter.applicationactive+=NewEventHandler (onapplicationactive); Application.addmessagefilter (MessageFilter); } /// <summary> ///This event is always executed when the program enters an idle period/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidOnapplicationidle (Objectsender, EventArgs e) { if(Timer! =NULL) timer. Start (); } /// <summary> ///when keyboard and mouse events, turn off timer/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidOnapplicationactive (Objectsender, EventArgs e) { if(Timer! =NULL) {timer. Stop (); Endidle (); } } Private voidEndidle () { This. Lbl_idle. Text ="end of idle period, enter active period"; stringstrpath = Application.startuppath +@"Test.txt"; using(StreamWriter SW =NewStreamWriter (strpath,true) ) {SW. WriteLine ("start active period, current time:"+DateTime.Now); Sw. Close (); } } } Public classFormmessagefilter:imessagefilter { Public EventEventHandler applicationactive; /// <summary> ///events are raised whenever you press the keyboard and mouse///because it is to monitor the keyboard and mouse, so all return false; ///return Ture: The value of the input will be cleared///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 BOOLPrefiltermessage (refMessage m) { if(M.msg = =0x100|| M.msg = =0x101|| (M.msg >0x199&& M.msg <0x207) || M.msg = =0x20a) { if(Applicationactive! =NULL) {applicationactive ( This,NewEventArgs ()); } } return false; } }}
Application.idle method