Application.idle method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.