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.
The following example shows the use of the System.Timers.Timer timer.
Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.linq;using system.text;using system.windows.forms;using system.timers;using system.runtime.interopservices;using system.threading;namespace timer001{public partial class Form1:form { Public Form1 () {InitializeComponent (); }//Instantiate Timer class System.Timers.Timer Atimer = new System.Timers.Timer (); private void Button1_Click (object sender, EventArgs e) {this. Settimerparam (); } private void Test (object source, System.Timers.ElapsedEventArgs e) {MessageBox.Show (datet Ime. Now.tostring ()); } public void Settimerparam () {//To time execution event atimer.elapsed + = new Elapsedeventhandler (test); Atimer.interval = 1000; Atimer.autoreset = true;//Executes once false, always executes true//whetherExecute System.Timers.Timer.Elapsed Event atimer.enabled = true; } }}
The effect is: the current time of the system pops up per second, such as:
The above is the C # Timer timer application content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!