C # Multithreading timed recurring calls to asynchronous threads that are System.Threading.Timer classes use small cases

Source: Internet
Author: User

The 1.system.threading.timer Timer provides a way to repeatedly invoke an asynchronous thread. There are multiple timer classes in the. Net BCL . such as the System.Windows.Forms.Timer class for Windows applications, such as the System.Timers.Timer class that can run on a user interface thread or on a worker thread. They are very different, and the System.Threading.Timer class here is a class that periodically invokes an asynchronous thread. Each time the timer is set, the system goes to the thread pool to open a thread that runs the provided callback method.

2. It is simple to call this timer class repeatedly to run asynchronous threads:

Timer MyTimer = new Timer (TimerCallback callback, Object state, uint duetime, uint period);

The parameters in the above method,Duetime is the first call to the asynchronous thread before the wait time, for an integer number of milliseconds, such as 2000, representing 2 seconds after the first call;

Period is a time interval between two invocations, for an integer number of milliseconds, such as 1000, that is called every 1 seconds (that is, an asynchronous thread is initiated, the newly initiated asynchronous thread does not affect the previously initiated thread, and all the originating threads continue to execute, as in the following example, until all threads of the user close the program end running. );

The state can be null or a reference to the object to pass in each time the asynchronous thread is invoked;

Callback is a method that passes a parameter of type object, the return value is a delegate type of void, and is customized by the user, with an example below.

3. Program Run results (console program)


4. Code

Using system;using System.threading;namespace repeatedly runs asynchronous threads with a timer {class Program {  int timescalled = 0;  void Display (object state)  {   Console.WriteLine (' {0} {1} keep running. ", (string) state, ++timescalled);             }  static void Main ()  {program   p = new program ();   Timer MyTimer = new timer (p.display, "Processing Timer event", 2000, 1000);//2 seconds after first call, every 1 seconds   //timer myTimer1 = new Ti Mer (P.display, "Processing Timer event", Timeout.infinite, 1000);//will never be called   //timer myTimer2 = new Timer (P.display, "Processing Timer event", timeout.infinite);//2 seconds after the first call, then do not call   Console.WriteLine ("Timer started.");   Console.ReadLine ();  } }}


C # Multithreading timed recurring calls to asynchronous threads that are System.Threading.Timer classes use small cases

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.