Timer in C#

https://docs.microsoft.com/en-us/dotnet/api/system.timers.timer?view=netframework-4.7.2

 Be aware that .NET includes four classes named Timer, each of which offers different functionality:

  • System.Timers.Timer (this topic): fires an event at regular intervals. The class is intended for use as a server-based or service component in a multithreaded environment; it has no user interface and is not visible at runtime.
  • System.Threading.Timer: executes a single callback method on a thread pool thread at regular intervals. The callback method is defined when the timer is instantiated and cannot be changed. Like the System.Timers.Timer class, this class is intended for use as a server-based or service component in a multithreaded environment; it has no user interface and is not visible at runtime.
  • System.Windows.Forms.Timer (.NET Framework only): a Windows Forms component that fires an event at regular intervals. The component has no user interface and is designed for use in a single-threaded environment.
  • System.Web.UI.Timer (.NET Framework only): an ASP.NET component that performs asynchronous or synchronous web page postbacks at a regular interval.

 


System.Threading.Timer is a simple, lightweight timer that uses callback methods and is served by thread pool threads. It is not recommended for use with Windows Forms, because its callbacks do not occur on the user interface thread.

System.Threading.Timer是一个简单的轻量级的计时器,通过使用callback 方法工作,并且由线程池上的线程提供服务。并不推荐将此计时器用于winform,因为它的回调方法不在UI线程上。

 

 

System.Windows.Forms.Timer is a better choice for use with Windows Forms.

System.Windows.Forms.Timer是用在winform上的更好选择

 

For server-based timer functionality, you might consider using System.Timers.Timer, which raises events and has additional features.

如果你需要使用基于服务器的计时器功能,你可以考虑使用System.Timers.Timer,它可以触发事件,并且有额外的功能

 

 

在winform中使用计时器的时候,会遇到以下问题:

如果你在计时器中刷新窗体,有使用Invoke方法的话,会出现提示,无法访问已释放的对象。

那么可以在窗体的关闭事件中,调用timer的Dispose方法。

posted @ 2015-04-29 10:34  ChuckLu  阅读(281)  评论(0编辑  收藏  举报