Timer object in. NET Framework

Source: Internet
Author: User

Regardless of the platform field, Timer objects play an important role in daily development. In. net, if you do not pay special attention to it, you will not find that there are three timer types in. NET Framework, respectively:

System. Timers

System. threading

System. Windows. Forms

Three different namespaces. (Here We Are Not counting the new timer in ASP. NET Ajax. This timer is a timer object for the client .) So I believe that many people will not be able to understand the timer function. Why should we define so many different timer types? Aside from its slightly different usage, we will make a comparison based on msdn's description of the three different Timers:

    1. System. Timers. Timer: system. componentmodel. component, isupportinitialize

First of all, this timer type is a standard timer object. For the time being, we consider it as a namespace because it exists in SystemProgramSet. In msdn, it is defined as a server-based timer (server-based timer), which is designed for auxiliary threads in a multi-threaded environment. The server timer can be moved between threads to processElapsedEvent, so it is the most accurate timer. We can simply understand it as the execution mechanism of Windows Services, as long as the host thread is not released, system. timers. timer will remain active until you manually stop or the host thread ends.

On msdn, there is a saying"ElapsedThe event is triggered on the threadpool thread. IfElapsedEvent processing time ratioIntervalLong, in anotherThreadpoolThe thread will trigger this event again. Therefore, the event handler should be reentrant ."That is to say, when each elapsed event processing arrives at the next round of time, the timer object will still start the elapsed event processing in another thread. The consequence of this mechanism may cause your timer to be terminated, but the elapsed event will be executed again. The original Article of msdn:"When a thread calls the stop method or sets the enabled attribute to false, it can run the event processing method on another thread. This may cause the elapsed event to be triggered after the timer is stopped ." In this case, if you don't want it to happen, you may have to do some extra work to avoid it. This mechanism also applies to system. Threading. Timer.

    1. System. Threading. Timer: financialbyrefobject, idisposable

This is a lightweight timer, which is different from system. Timers. Timer in terms of usage:

    • Use the callback mechanism instead of the event mechanism.
    • In the constructor, you can specify the first execution time (after construction or after modification) and interval execution time. The two time periods (duetime, period) can be different.
    • It is an interface with no start and end control, starting from the build to the end of the release.

It is based on the threadpool thread mechanism and follows the same principle (red part) of the above system. Timers. Timer ). At the same time, it must be noted that it does not start or stop (with the Release Interface dispose. In its lifecycle, it must be referenced by other objects. Once it is not referenced by any object, it means that the timer object becomes an inaccessible object and will be recycled by GC. Original msdn explanation:

"As long as you are usingTimer, You must keep the reference to it. If noTimer. Even ifTimerIt is still active and will be recycled ."

There is also a saying in msdn:

"System. threading .. ::. timer is a simple, lightweight timer that uses callback methods and is served by thread pool threads. it is not recommended for use with Windows Forms, because its callbacks do not occur on the user interface thread. system. windows. forms .. ::. timer is a better choice for use with Windows Forms. for server-based timer functionality, you might consider using system. timers .. ::. timer, which raises events and has additional features."

It tells us that system. Threading. Timer is a simple and lightweight timer that uses the callback mechanism and thread pool mechanism. We do not recommend using this object in Windows Forms, because the UI thread does not trigger the callback function and is replaced by windows. forms. timer. If you want to use the server timer-based function, we recommend that you use system. timers. timer.

    1. System. Windows. Forms. Timer: system. componentmodel. Component

This timer specifically serves windows forms, which is quite different in terms of mechanism and principle from the previous two. The interface is similar to system. Timers. timer and has some features of the Windows form control. At the same time, its precision is not very high in design.

"Timer is used to trigger events at user-defined event intervals. The Windows timer is designed for a Single-threaded environment, where the UI thread is used for processing. It requires the userCodeThere is an available UI message pump that is always operated in the same thread, or calls are blocked to another thread.

When using this timer, use the tick event to perform the round robin operation, or display the startup screen within the specified time. When the enabled attribute is set to true and the interval attribute is greater than 0, a tick event is triggered. The interval is set based on the interval attribute ."

Among the three timer objects, there will be some commonalities and different focuses, and we hope that through such a comparison, everyone can be right. net Framework.

Related Article

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.