About the Timer class in C # There are 3 of timers in C #:
1. Definition in System.Windows.Forms
2. defined in the System.Threading.Timer class
3. defined in the System.Timers.Timer class
System.Windows.Forms.Timer is applied to WinForm, which is implemented through the Windows messaging mechanism, similar to the Timer control in VB or Delphi, implemented internally using the API SetTimer. The main drawback is that the timing is imprecise and 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, light-weight, accurate timing, no special requirements for applications, messages.
System.Timers.Timer can also be applied to WinForm, completely replacing the timer control above. Their disadvantage is that they do not support direct drag-and-drop and require manual coding.
This article url:http://www.bianceng.cn/programming/csharp/201410/45596.htm
The following example shows the use of the System.Timers.Timer timer.
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; usingSystem.Timers; namespaceTimer001 { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } //instantiating a timer classSystem.Timers.Timer Atimer =NewSystem.Timers.Timer (); Private voidButton1_Click (Objectsender, EventArgs e) { This. Settimerparam (); } Private voidTestObjectsource, System.Timers.ElapsedEventArgs E) {MessageBox.Show (DateTime.Now.ToString ()); } Public voidSettimerparam () {//execution of events by Timeatimer.elapsed + =NewElapsedeventhandler (test); Atimer.interval= +; Atimer.autoreset=true;//executes false once, always executes true//whether to perform the System.Timers.Timer.Elapsed eventatimer.enabled =true; } } }
Example of using a timer timer in C #