Differences between system. Windows. Forms. timer and system. Timers. Timer

Source: Internet
Author: User
Tags net thread

. NET Framework provides three types of Timer:

    • System. Windows. Forms. Timer
    • System. Timers. Timer
    • System. Threading. Timer

By default, vs. NET 2005 only has one timer control, but it is a system. Forms. Timer control. To use the system. Timers. Timer control, right-click the toolbox and add it manually.

Add steps: Right-click the toolbox and choose add item. Find the control whose namespace is system. Timers. Timer, select it, and OK.

Here we will briefly introduce the differences between the two timer types.

System. Windows. Forms. Timer is a much-used timer. After timer start, the evnethandler attached to the tick event is called regularly (according to the set interval. In the eventhandler of the timer, you can directly obtain and modify the UI elements without any problems-because the timer is actually called on the UI thread itself. It is precisely for this reason that long blocking calls are performed in the eventhandler of the timer, and the interface response will be blocked. The following is a simple example:

  
  

Public class mainform: Form
{

Private void mainform_load (Object sender, eventargs E)
{
Timer. interval = 1000;
Timer. Tick + = delegate (Object o, eventargs ARGs)
{
Dowork ();
};
Timer. Start ();
}

Private void dowork ()
{
For (INT I = 0; I <10; I ++)
{
System. Threading. thread. Sleep (1000 );
}
}
System. Windows. Forms. Timer timer = new system. Windows. Forms. Timer ();
}


In this example, the dowork method will block 10 seconds, and the UI will lose response within these 10 seconds. By using system. Timers. Timer, you can solve this problem. Because system. timers. timer is in. net thread pool, rather than running directly on the UI thread, so the time-consuming computing in the eventhandler of the timer will not cause the UI to lose response. However, there are two points to note:

  1. because system. timers. timer is not running on the UI thread, so if you want to update the UI element in the eventhandler of the timer, You need to perform a thread switch, in winform development, the invoke method of the UI element is generally used:
        
       

    private void dowork ()
    {< br> for (INT I = 0; I <10; I ++)
    {< br> system. threading. thread. sleep (1000);
    }< br> This. invoke (New updateuicallback (updateui);
    }

    private delegate void updateuicallback ();

    private void updateui ()
    {
    }

  2. System. Timers. timer has a property:Synchronizingobject. If this property is set (generally a form), the call to the eventhandler attached to the timer will be performed on the thread that creates the UI element (generally the UI thread ). It is worth noting that if you drag system. Timers. timer to form through the winform designer, this property will be automatically set. In this case, Timer works the same way as system. Windows. Forms. Timer: a long call will block the interface.

System. Windows. Forms. Timer
1. It is a form-based timer.
2. After creation, you can use interval to set the span between tick and use the delegate (delegate) to hook the tick event.
3. Call the start and stop methods to start and stop
4. It is completely based on the UI thread, so some UI-related operations will be performed in this timer.
5. Long UI operations may cause some tick loss

System. Timers. Timer
1. The elapsed event is not a tick event.
2. Like system. Windows. Forms. Timer, use the start and stop methods.
3. The autoreset attribute determines whether the timer starts an event and stops, or enters the start/Wait loop. System. Windows. Forms. Timer does not have this attribute.
4. Set a synchronization object for the UI control to initiate an event on the UI thread of the control.

For the differences between the two and system. Threading. Timer, see worker:
System. Windows. Forms. Timer is a clear choice for user interface programming. The choice between the other two is not very obvious. If it must be in icontainer, you should select system. Timers. Timer. If the features of system. Timers. timer are not used, we recommend that you select system. Threading. Timer because it is lightweight.

Timer is waiting for execution first. If we want to achieve the effect of first execution and then waiting, set the default interval Interval= 100, or less is 1 (cannot be 0), and then the change interval in the event is the expected value.

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.