C # Timers

Source: Internet
Author: User

There are 3 of timer classes in C #

    • 1. Definition in System.Windows.Forms
    • 2. defined in the System.Threading.Timer class
    • 3. defined in the System.Timers.Timer class

System.Windows.Forms.Timer is applied to WinForm, which is implemented through the Windows messaging mechanism, similar to the Timer control in VB or Delphi, implemented internally using the API SetTimer. The main drawback is that the timing is imprecise and there must be a message loop that the console application (console application) cannot use.
System.Timers.Timer and System.Threading.Timer are very similar, they are implemented through the. NET Thread pool, light-weight, accurate timing, no special requirements for applications, messages. System.Timers.Timer can also be applied to WinForm, completely replacing the timer control above. Their disadvantage is that they do not support direct drag-and-drop and require manual coding.

Example 1:
Using the System.Timers.Timer class

usingSystem.Windows.Forms;namespacewindowsformsapplication1{ Public Partial classForm2:form {Private ReadOnlySystem.Timers.Timer _timer =NewSystem.Timers.Timer ( +);//instantiate the Timer class, set a time interval of 10000 milliseconds;         PublicForm2 () {InitializeComponent (); _timer. Elapsed+=NewSystem.Timers.ElapsedEventHandler (theout);//execution of events at time of arrival;_timer. AutoReset =true;//Whether the setting is executed once (false) or always executed (true);_timer. Enabled =true;//whether to execute the System.Timers.Timer.Elapsed event;        }         Public voidTheout (Objectsource, System.Timers.ElapsedEventArgs E) {MessageBox.Show (@"ok!"); }    }}

Example TimerCallback delegate for 2:system.threading.timer class
System.Threading.Timer is a timer that uses callback methods and is serviced by thread pool threads, simple and resource-efficient. Whenever you use a Timer, you must keep a reference to it. For any managed object, the timer is garbage collected if there is no reference to the timer. Even if the Timer is still active, it will be recycled. When the timer is no longer needed, use the Dispose method to release the resource held by the timer. Use the TimerCallback delegate to specify the method that you want the Timer to execute. The timer delegate is specified when the timer is constructed and cannot be changed. Instead of executing in the thread that created the timer, this method executes in the system-provided thread pool threads. When you create a timer, you can specify the amount of time to wait before the method is first executed (the cutoff time) and the amount of time to wait for the execution period thereafter (the time period). You can change these values or disable timers by using the changes method. Demo Application: Scenario: After the Windows Form program automatically performs a task, you want its Windows Form to close automatically.

Code Design:

(1) First declare the Timer variable: private System.Threading.Timer timerclose;

(2) Add the following timer instantiation code after the above automatic execution code:

// Create a timer thread to start it New System.Threading.Timer (newthis0);

Timer constructor Parameter Description: Callback: A TimerCallback delegate that represents the method to execute. State: An object that contains the information to be used by the callback method, or a null reference (Nothing in Visual Basic). Duetime: The amount of time, in milliseconds, to delay before calling callback. Specifies timeout.infinite to prevent the timer from starting. Specify 0 (0) to start the timer immediately. Period: The time interval (in milliseconds) at which the callback is called. Specifies that timeout.infinite can disable periodic termination.

(3) Define the method to be executed by the TimerCallback delegate:

Private void Timercall (object  obj) {      timerclose.dispose ();        This . Close ();}

C # Timers

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.