There are three kinds of timers in. NET, one is the timer control under the System.Windows.Forms namespace, it inherits directly from Componet, and the second is the timer class under the System.Timers namespace.
Timer Control: The timer control only binds the tick event and sets the Enabled=true to automatically timer, stopping timings can be controlled with stop (), and after Stop (), you can start the timer with the start () method if you want to do it again. The Timer control and its form belong to the same thread;
System.Timers.Timer class: Define a System.Timers.Timer object, bind the elapsed event, start the timer through the start () method, or stop the timer by means of the stop () method or Enable=false. The Autoreset property sets whether to repeat the timings. The elapsed event binding is quite another thread, meaning that the controls that are Chengri to other lines cannot be accessed in elapsed bound events.
System.Threading.Timer: When defining this class, there are four main parameters. TimerCallback, a delegate with a return value of void, the parameter is object, and a method that the timer executes. Object state, the timer executes the parameters of the method. int duetime The amount of time, in milliseconds, to delay before the callback is invoked. Specifies timeout.infinite to prevent the timer from starting. Specify 0 (0) to start the timer immediately.
int Period, the time interval, in milliseconds, to invoke the callback. Specifies that timeout.infinite can be disabled for periodic termination.
In these three timers, the first timer is in the same thread as the form where it is located, so the execution is inefficient. The second and third timers execute a new thread, so execution efficiency is better than the first timer. Therefore, the second and third types are recommended when using timers.
Here are three examples of the use of timers
1) Timer Control
public partial class Timer:form
{
int count = 0;
Public Timer ()
{
InitializeComponent ();
The Timer control is available
This.timer1.Enabled = true;
Sets the time interval at which the timer control's Tick event triggers
This.timer1.Interval = 1000;
Stop timing
This.timer1.Stop ();
}
private void Timer1_Tick (object sender, EventArgs e)
{
Count + 1;
This.tbTimer.Text = count. ToString ();
}
private void Btstart_click (object sender, EventArgs e)
{
Start the timer.
This.timer1.Start ();
}
private void Btstop_click (object sender, EventArgs e)
{
Stop timing
This.timer1.Stop ();
}
}
2) System.Timers.Timer
public partial class Timer:form
{
int count = 0;
Private System.Timers.Timer Timer = new System.Timers.Timer ();
Public Timer ()
{
InitializeComponent ();
Set Timer available
Timer. Enabled = true;
Set timer
Timer. Interval = 1000;
Sets whether to repeat the timings, and if this property is set to False, only the Timer_elapsed method is executed once.
Timer. Autoreset = true;
Timer. Elapsed+=new System.Timers.ElapsedEventHandler (timer_elapsed);
}
private void Timer_elapsed (object sender, System.Timers.ElapsedEventArgs e)
{
Count + 1;
SETTB (count. ToString ());
}
private void Btstart_click (object sender, EventArgs e)
{
Timer. Start ();
}
private void Btstop_click (object sender, EventArgs e)
{
Timer. Stop ();
}
Private delegate void Settbmethodinvok (string value);
private void Settb (string value)
{
if (this. invokerequired)
{
This. Invoke (New Settbmethodinvok (SETTB), value);
}
Else
{
This.tbTimer.Text = value;
}
}
}
3) System.Threading.Timer
public partial class Timer:form
{
int count = 0;
System.Threading.Timer Timerthr;
Private delegate void Settbmethodinvoke (object state);
Public Timer ()
{
InitializeComponent ();
Initializes a timer, and at first count, the time interval to invoke callback is 500 milliseconds
Timerthr = new System.Threading.Timer (new TimerCallback (SETTB), NULL, Timeout.infinite, 500);
}
public void Settb (object value)
{
if (this. invokerequired)
{
This. Invoke (New Settbmethodinvoke (SETTB), value);
}
Else
{
Count + 1;
This.tbTimer.Text = count. ToString ();
}
}
private void Btstart_click (object sender, EventArgs e)
{
Start the timer.
Timerthr.change (0, 500);
}
private void Btstop_click (object sender, EventArgs e)
& nbsp; {
/stop Timing
Timerthr.change (Timeout.infinite, 500);
}
}