如何在 C# 中重置计时器? [英] How to reset a timer in C#?

查看:182
本文介绍了如何在 C# 中重置计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道三个 Timer 类,System.Threading.TimerSystem.Timers.TimerSystem.Windows.Forms.Timer,但这些都没有 .Reset() 函数可以将当前经过的时间重置为 0.

There are three Timer classes that I am aware of, System.Threading.Timer, System.Timers.Timer, and System.Windows.Forms.Timer, but none of these have a .Reset() function which would reset the current elapsed time to 0.

是否有具有此功能的 BCL 类?有没有一种非黑客的方式来做到这一点?(我想也许改变它的时间限制可能会重置它)考虑重新实现具有此功能的 Timer 类会有多困难,或者如何使用 BCL 类之一可靠地实现它?

Is there a BCL class that has this functionality? Is there a non-hack way of doing it? (I thought perhaps changing the time limit on it might reset it) Thought on how hard it would be to reimplement a Timer class that had this functionality, or how to do it reliably with one of the BCL classes?

推荐答案

我总是...

myTimer.Stop();
myTimer.Start();

...这是黑客攻击吗?:)

... is that a hack? :)

根据评论,关于 Threading.Timer,它是 更改方法...

Per comment, on Threading.Timer, it's the Change method ...

dueTime 类型:System.Int32延迟之前的时间量调用指定的回调方法在构造 Timer 时,在毫秒.指定Timeout.Infinite 防止定时器从重新启动.指定零(0) 立即重启定时器.

dueTime Type: System.Int32 The amount of time to delay before the invoking the callback method specified when the Timer was constructed, in milliseconds. Specify Timeout.Infinite to prevent the timer from restarting. Specify zero (0) to restart the timer immediately.

这篇关于如何在 C# 中重置计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆