[C #]. The use instance of several timer in net _c# tutorial

Source: Internet
Author: User
Tags datetime readline time interval

This blog will be combed. NET 4 timer classes, and their usage.

1. System.Threading.Timer

Public Timer (TimerCallback callback, object state, int duetime, int period);

The callback delegate will be repeated over the period interval, and the state parameter can pass in the object that you want to handle in the callback delegate, duetime how long after callback starts execution, period how often the callback identifies.

Using System.Threading;
System.Threading.Timer

Timer timer = new Timer (delegate
{
 Console.WriteLine ($) Timer Thread: { THREAD.CURRENTTHREAD.MANAGEDTHREADID} ");

 Console.WriteLine ($ "is Thread Pool: {Thread.CurrentThread.IsThreadPoolThread}");

 Console.WriteLine ("Timer Action.");
},
null,
Watts,
1000
);

Console.WriteLine ("Main Action.");
Console.WriteLine ($ "Main Thread: {Thread.CurrentThread.ManagedThreadId}");

Console.ReadLine ();

The timer callback method execution is performed in a new thread in another threadpool.

2. System.Timers.Timer

System.Timers.Timer and System.Threading.Timer provide more properties than the

INTERVAL Specifies the time interval at which the elapsed event is executed;

Elapsed Specifies events that are executed regularly;

Enabled for Start/stop Timer;

Start timer

Stop Stop Timer

System.Timers.Timer Timer = new System.Timers.Timer ();

Timer. Interval = +;

Timer. Elapsed + = Delegate
{
 Console.WriteLine ($ "Timer Thread: {Thread.CurrentThread.ManagedThreadId}");

 Console.WriteLine ($ "is Thread Pool: {Thread.CurrentThread.IsThreadPoolThread}");

 Console.WriteLine ("Timer Action");

 Timer. Stop ();
};

Timer. Start ();

Console.WriteLine ("Main Action.");
Console.WriteLine ($ "Main Thread: {Thread.CurrentThread.ManagedThreadId}");

Console.ReadLine ();

The Timer elapsed recurring task is performed in a threadpool thread.

3. System.Windows.Forms.Timer

INTERVAL Specifies the time interval at which the elapsed event is executed;

Tick Specifies events that are executed regularly;

Enabled for Start/stop Timer;

Start timer

Stop Stop Timer

Use System.Windows.Forms.Timer to update the label time in the form.

Using System.Windows.Forms;
Public Form1 ()
{
 InitializeComponent ();
 This. Load + = delegate
 {
  Timer timer = new timer ();

  Timer. Interval = +;

  Timer. Tick + = delegate
  {
   System.Diagnostics.Debug.WriteLine ($ "Timer Thread: { SYSTEM.THREADING.THREAD.CURRENTTHREAD.MANAGEDTHREADID} ");

   System.Diagnostics.Debug.WriteLine ($ "is Thread Pool: {System.Threading.Thread.CurrentThread.IsThreadPoolThread}") ;

   This.lblTimer.Text = DateTime.Now.ToLongTimeString ();
  };

  Timer. Start ();

  System.Diagnostics.Debug.WriteLine ($ "Main Thread: {System.Threading.Thread.CurrentThread.ManagedThreadId}");
}

The event thread executed in the Timer tick event is the same as the thread of the main form, and no new threads are created (or use a threadpool thread) to update the UI. Here's a change to the code, using System.Timers.Timer to update the time on the UI, as follows,

Public Form1 ()
{
 InitializeComponent ();

 This. Load + = delegate
 {
  System.Timers.Timer Timer = new System.Timers.Timer ();

  Timer. Interval = +;

  Timer. Elapsed + = delegate
  {
   System.Diagnostics.Debug.WriteLine ($ "Timer Thread: { SYSTEM.THREADING.THREAD.CURRENTTHREAD.MANAGEDTHREADID} ");

   System.Diagnostics.Debug.WriteLine ($ "is Thread Pool: {System.Threading.Thread.CurrentThread.IsThreadPoolThread}") ;

   This.lblTimer.Text = DateTime.Now.ToLongTimeString ();
  };

  Timer. Start ();

  System.Diagnostics.Debug.WriteLine ($ "Main Thread: {System.Threading.Thread.CurrentThread.ManagedThreadId}");
}

Very familiar with a mistake. Because the label is created by the UI thread, modifying it needs to be done in the UI thread. elasped execution in System.Timers.Timer is performed in a newly created thread in ThreadPool. So there will be the above error.

4. System.Windows.Threading.DispatcherTimer

Properties and methods are similar to System.Windows.Forms.Timer.

Using System.Windows.Threading;

Public MainWindow ()
{
 InitializeComponent ();

 This. Loaded + = delegate
 {
  //dispatchertimer

  dispatchertimer timer = new DispatcherTimer ();

  Timer. Interval = Timespan.fromseconds (1);

  Timer. Start ();

  Debug.WriteLine ($ "Main Thread Id: {Thread.CurrentThread.ManagedThreadId}");

  Timer. Tick + = delegate
  {
   tbtime.text = DateTime.Now.ToLongTimeString ();

   Debug.WriteLine ($ "Timer Thread Id: {Thread.CurrentThread.ManagedThreadId}");

   Timer. Stop ();};};}


Tick event execution in DispatcherTimer is done in the main thread.

One thing to note when using DispatcherTimer is that the DispatcherTimer tick event is in the dispatcher queue, and when the system is in high load, there is no guarantee that it will execute in the interval time period, and there may be a slight delay. However, it is absolutely guaranteed that the execution of tick will not be older than the time set by interval. If the accuracy of tick execution time is high, the priority of DispatcherTimer can be set. For example:

DispatcherTimer timer = new DispatcherTimer (dispatcherpriority.send);

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.