Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
113 views
in Technique[技术] by (71.8m points)

c# - Why there are 5 Versions of Timer Classes in .NET?

Why are there five timer classes in the .Net framework, namely the following:

  1. System.Timers.Timer
  2. System.Threading.Timer
  3. System.Windows.Forms.Timer
  4. System.Web.UI.Timer
  5. System.Windows.Threading.DispatcherTimer

Why are there several versions of the Timer class? And what are the differences between them?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's a description of the primary timers and the points that i find to be the most noteworthy.

Winforms.Timer

  • ticks on UI thread not guaranteed to ticket at a specific time
  • ticks delayed until UI thread is idle
  • will skip ticks if the UI thread is busy

DispatcherTimer

  • invoked on UI thread
  • can set priority for what level of 'idle' is required to generate a tick
  • will skip ticks

Threading.Timer

  • ticks on a worker thread from threadpool - no option for specifying thread
  • ticks are always fired on time
  • none are skipped - you must guard against new ticks while you're still processing a former tick
  • unhandled exceptions will crash the application

Timers.Timer

  • wrapper around threading timer
  • ticks on a worker thread taken from the CLR threadpool
  • can force to tick on a specific thread by supplying a SynchronizationObject
  • ticks are always fired on time
  • none are skipped
  • silently eats exceptions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...