C # three types of Timer

Source: Internet
Author: User
Zookeeper

There are three timers in. net. One is the timer control in the system. Windows. Forms namespace, which directly inherits from componet; the other is the Timer class in the system. Timers namespace.

Timer control: the timer control is automatically timed only when it is bound to a tick event and is set to Enabled = true. You can use stop () to stop the timer. After stopping the timer through stop, if you want to re-timer, you can use the START () method to start the timer. The timer control and its form belong to the same thread;

 

System. Timers. Timer class: defines a system. Timers. Timer object, binds elapsed events, starts timing using the START () method, and stops timing using the stop () method or enable = false method. The autoreset attribute sets whether the timer is repeated. The binding of the elapsed event is equivalent to opening another thread. That is to say, the events bound to elapsed cannot access controls in other threads.

 

System. Threading. Timer: this class has four parameters. Timercallback. the return value is void and the parameter is the object delegate. It is also the method for timer execution. Object state, the parameter of the timer execution method. Int duetime, the time delay before calling callback (in milliseconds ). Specify timeout. Infinite to prevent the timer from starting timing. Specify zero (0) to start the timer immediately.

Int period, the call interval (in milliseconds ). The specified Timeout. Infinite can disable periodic termination.

 

Among the three timers, the first timer is in the same thread as the form, so the execution efficiency is not high. In the second and third ways, the timer execution method is to open a new thread, so the execution efficiency is better than the first timer. Therefore, the second and third types are recommended when you use a timer.

The following example shows how to use a timer in the third-party architecture.


1) Timer control

Public partial class Timer: Form
{

Int COUNT = 0;
Public timer ()
{
Initializecomponent ();

// Timer control available
This. timer1.enabled = true;

// Set the interval for triggering tick events of the timer control
This. timer1.interval = 1000;

// Stop timing
This. timer1.stop ();
}

Private void timereffectick (Object sender, eventargs E)
{
Count + = 1;
This. tbtimer. Text = count. tostring ();
}

Private void btstart_click (Object sender, eventargs E)
{
// Start timing
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 availability
Timer. Enabled = true;

// Set timer
Timer. interval = 1000;

// Set whether to repeat the time. If this attribute 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 ();

// Initialize a timer. If the timer is not started, the call interval 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 timing
Timerthr. Change (1, 0,500 );
}

Private void btstop_click (Object sender, eventargs E)
{
// Stop timing
Timerthr. Change (timeout. Infinite, 500 );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.