System. Timers. Timer regularly executes program sample code

  • 2020-06-12 08:51:35
  • OfStack

System.Timers.Timer regularly executes the program
 
System.Timers.Timer t = new System.Timers.Timer(5000); // Set the time interval to 5 seconds  
private void Form1_Load(object sender, EventArgs e) 
{ 
t.Elapsed += new System.Timers.ElapsedEventHandler(Timer_TimesUp); 
t.AutoReset = false; // At the appointed time Elapsed The event is the trigger 1 Time ( false ), or 1 Straight trigger ( true )  
} 
private void btnStart_Click(object sender, EventArgs e) 
{ 
t.Enabled = true; // Whether the trigger Elapsed The event  
t.Start(); 
} 
private void Timer_TimesUp(object sender, System.Timers.ElapsedEventArgs e) 
{ 
// Time of arrival 5 Seconds triggers the event output  Hello World!!!! 
System.Diagnostics.Debug.WriteLine("Hello World!!!!"); 
} 
private void btnStop_Click(object sender, EventArgs e) 
{ 
t.Stop(); 
System.Diagnostics.Debug.WriteLine(" Not up to the appointed time 5 Seconds end early!! "); 
} 

web's timed cleanup cache can be removed
 
System.Timers.Timer t = new System.Timers.Timer(5000); // Set the time interval to 5 seconds  
t.Elapsed += new System.Timers.ElapsedEventHandler(Timer_TimesUp); 
t.AutoReset = false; // At the appointed time Elapsed The event is the trigger 1 Time ( false ), or 1 Straight trigger ( true )  
t.Enabled = true; // Whether the trigger Elapsed The event  
t.Start(); 

Line 5 code is put into Application_Start of ES11en.cs. When web is started, it is started.
If it is a timing for a logical function, code can be placed in the static constructor of the logical function's class. The timing starts naturally when the static constructor is called on the first execution of the logical class.

Related articles: