System. Timers. Timer scheduled execution Program
Copy codeThe Code is as follows:
System. Timers. Timer t = new System. Timers. Timer (5000); // set the interval to 5 seconds.
Private void Form1_Load (object sender, EventArgs e)
{
T. Elapsed + = new System. Timers. ElapsedEventHandler (Timer_TimesUp );
T. AutoReset = false; // whether the Elapsed event is triggered once (false) or always (true) at the specified time)
}
Private void btnStart_Click (object sender, EventArgs e)
{
T. Enabled = true; // whether to trigger the Elapsed event
T. Start ();
}
Private void Timer_TimesUp (object sender, System. Timers. ElapsedEventArgs e)
{
// Triggered the event at the specified time of arrival in 5 seconds. The output is Hello World !!!!
System. Diagnostics. Debug. WriteLine ("Hello World !!!! ");
}
Private void btnStop_Click (object sender, EventArgs e)
{
T. Stop ();
System. Diagnostics. Debug. WriteLine ("It has not ended 5 seconds before the specified time !!! ");
}
You can set
Copy codeThe Code is as follows:
System. Timers. Timer t = new System. Timers. Timer (5000); // set the interval to 5 seconds.
T. Elapsed + = new System. Timers. ElapsedEventHandler (Timer_TimesUp );
T. AutoReset = false; // whether the Elapsed event is triggered once (false) or always (true) at the specified time)
T. Enabled = true; // whether to trigger the Elapsed event
T. Start ();
Put the five lines of code in Application_Start of gloab. cs. When the web is started, the code is started;
If a logic function is scheduled, you can place the code to the static constructor of the class of the logic function. When the logic class is executed for the first time, the static constructor will be called, the instance is started on a regular basis.