In C #, there are three Timer classes:
1.system.windows.forms.timer
2.system.threading.timer
3.system.timers.timer
System.Windows.Forms.Timer is applied to WinForm, it is implemented through the Windows messaging mechanism, similar to the Timer control in VB or Delphi, the internal use of API SetTimer implementation. Its main disadvantage is that the timing is imprecise and that there must be a message loop that the console application (console application) cannot use.
System.Timers.Timer and System.Threading.Timer are very similar, they are implemented through the. NET Thread Pool, lightweight, timed accurately, for applications, There are no special requests for messages. System.Timers.Timer can also be applied to WinForm to completely replace the timer control above. Their disadvantage is that they do not support direct drag-and-drop and require manual coding.
Example:
Using the System.Timers.Timer class
private void Button2_Click (object sender, EventArgs e)
{
System.Timers.Timer t = new System.Timers.Timer ( 10000)//Instantiate timer class, set interval time is 10000 milliseconds;
t.elapsed + + new System.Timers.ElapsedEventHandler (theout); Executes the event when the time is reached;
T.autoreset = true;//Whether the setting is executed once (false) or always (true);
t.enabled = true;// Whether to execute the System.Timers.Timer.Elapsed event;
}
private void Ontimedevent (object source, Elapsedeventargs e)
{
MessageBox.Show ("OK.");
}