Common timers in C #

Source: Internet
Author: User

1, System.Timers.Timer and System.Windows.Forms.Timer, its minimum identification is 1/18s.

2, timeGetTime, his minimum identification can reach 5ms.

3, System.Environment.TickCount, it is recognized as a minimum millisecond level.

4, QueryPerformanceCounter, it is recognized as a minimum of 1ms.

System.Timers.Timer

System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Timers.Timer

Generate recurring events in the application.

The Timer component is a server-based timer that enables you to specify in the application to raise The periodic interval of the Elapsed event. You can then provide general processing by handling this event. For example, suppose you have a critical server that must remain operational 24 hours a day, 7 days a week. You can create a service that uses a Timer to periodically check the server and ensure that the system is turned on and running. If the system does not respond, the service can attempt to restart the server or notify the administrator.

Server-based Timer is designed for use with worker threads in a multithreaded environmentserver timers can move between threads to handle the Elapsed event that is raised, which can be compared to Windows timers raise events more precisely and on time.

based on   Interval   property value, Timer component raised Elapsed   event. You can handle this event to perform the necessary processing. For example, suppose you have an online sales application that continues to post sales orders to a database. The service that compiles the instructions to be delivered batches orders, rather than processing each order separately. You can use a Timer to start batch processing every 30 minutes.

Note

when   AutoReset   when set to False, the Timer is only in the first   Interval   raised once   Elapsed event. To keep the   Interval   time interval raises   Elapsed   event, leave AutoReset   set to True.

in the. NET Framework version 2.0 and earlier, the Timer component captures   Elapsed   the event handler for the event throws all exceptions and suppresses the display of these exceptions. This behavior may change in future versions of the. NET Framework.

if   synchronizingobject   property is null, then   ThreadPool   thread throws   Elapsed   Event. If   Elapsed event processing time is greater than   Interval   long, in another   ThreadPool   thread will raise this event again. In this case, the event handler should be reentrant.

Note

called on a thread Span class= "Apple-converted-space" >  Stop   method or   Enabled   The property is set to False while the event-handling method can be run on another thread. This may cause the   to be raised after the timer stops; Elapsed   event.   Stop   The code example of the method demonstrates a way to avoid this race condition.

even if   synchronizingobject   is not null, Elapsed   in   Dispose   or Stop   after method call or Enabled   After the property is set to False, the event can occur because the outgoing Elapsed   event signals are always queued for execution thread pool threads. One way to resolve this race condition is to set a flag to notify Elapsed   The event handler for the event ignores the subsequent event.

If you use a timer with a user interface element such as a form or control, the user interface element that does not have a timer placed, specifies that a timer form or control is included in the The synchronizingobject property to marshal the event to the user interface thread.

The Timer is not visible at run time.

for a list of initial property values for instances of the Timer, see Timer constructor.

Attention

applied to this type or member. The Hostprotectionattribute attribute has the following Resources property values: Synchronization | Externalthreading. hostprotectionattribute does not affect desktop applications (desktop applications typically start by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see hostprotectionattribute class or SQL Server programming and host protection features .

Example Timer1

System.Windows.Forms.Timer

System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Timer

The Timer is used to trigger events at user-defined event intervals. Windows timers are designed for single-threaded environments where the UI thread is used to perform processing. It requires that the user code have an available UI message pump and always operate on the same thread, or marshal the call to another thread.

when using this timer, use the The Tick Event performs a polling operation or displays a splash screen for a specified period of time. Whenever the Enabled property is set to True and the Interval property is greater than 0 o'clock, the The Tick event, which is raised based on the Interval property setting.

This class provides methods for setting the time interval and starting and stopping timers.

Attention

The Windows Forms Timer component is a single-threaded component with a precision limit of 55 milliseconds. If you need a more precise multithreaded timer, use the Timer class in the System.Timers namespace .

Example Timer2

The latter applies to the timing

QueryPerformanceCounter &

timeGetTime &

System.Environment.TickCount

Maximum resolution Timer

Can be used to pinpoint code performance bottlenecks.

Startup value: 3516284.3498
End Value: 3516284.3521
QueryPerformanceCounter min. Resolution: 1/1193182 seconds
API Cost: 1.92761875388667E-05 seconds

GetTickCount minimum resolution: Ten MS
timeGetTime minimum resolution: Ten MS

Common timers in C #

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.