×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

Okay, that is tricky but good question. For those 4 timer classes, the key to distinguish is UI, threading and accuracy.

System.Web.UI.Timer is a ASP.NET control and you can ignore it.
System.Windows.Forms.Timer is a single thread timer for UI thread and lower accuracy(per 55ms);
System.Threading is also a single thread timer for worker thread(or thread pool threads) but not for UI thread;
System.Timers.Timer is a server based timer, it is thread safe, higher accuracy.

Now the conclusion:
For UI, not high accuracy, System.Windows.Forms.Timer;
For background/worker thread, System.Threading.Timer;
For components serving as a server application, System.Timers.Timer;

Or you may also construct your own timer for more accurate options by p-invocation;
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / keychain(人间正道是沧桑),来聊聊.NET吧,我们先聊回字有多少种写法:)
    .NET里有多个Timer Class,全弄清楚不好整呢,有没有方便的方法?
    • Okay, that is tricky but good question. For those 4 timer classes, the key to distinguish is UI, threading and accuracy.
      System.Web.UI.Timer is a ASP.NET control and you can ignore it.
      System.Windows.Forms.Timer is a single thread timer for UI thread and lower accuracy(per 55ms);
      System.Threading is also a single thread timer for worker thread(or thread pool threads) but not for UI thread;
      System.Timers.Timer is a server based timer, it is thread safe, higher accuracy.

      Now the conclusion:
      For UI, not high accuracy, System.Windows.Forms.Timer;
      For background/worker thread, System.Threading.Timer;
      For components serving as a server application, System.Timers.Timer;

      Or you may also construct your own timer for more accurate options by p-invocation;
      • Good answer, thanks.