How to execute the program regularly by System.Timers.Timer
This article mainly introduces how System.Timers.Timer regularly executes the program. The introduction in the article is very detailed and has certain reference value. Interested friends must read it!
System.Timers.Timer
The 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 fires once (false) or all the time (true) at the specified time
}
private void btnStart_Click(object sender, EventArgs e)
{
t.Enabled = true; //Whether the Elapsed event is triggered
t.Start();
}
private void Timer_TimesUp(object sender, System.Timers.ElapsedEventArgs e)
{
//5 seconds after the specified time, trigger this event to output Hello World!!!
System.Diagnostics.Debug.WriteLine("Hello World!!!! ");
}
private void btnStop_Click(object sender, EventArgs e)
{
t.Stop();
System.Diagnostics.Debug.WriteLine("End 5 seconds before specified time!!! ");
}
The web's timed cleanup cache can be
The copy 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 fires once (false) or all the time (true) at the specified time
t.Enabled = true; //Whether the Elapsed event is triggered
t.Start();
Five lines of code into gloab.cs Application_Start, start web, start;
If it is the timing of a logic function, you can put the code into the static constructor of the logic function class. When the logic class is executed for the first time, the static constructor will be called, and the timing will start naturally.
The above is "System.Timers.Timer how to execute the program timing" all the content of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!