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方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值