据我所知,有三个Timer
类:System.Threading.Timer
、System.Timers.Timer
和System.Windows.Forms.Timer
,但它们都没有一个.Reset()
函数可以将当前运行时间重置为0。
是否有具有此功能的BCL类?有没有一种非黑客的方式呢?(我想也许更改它的时间限制可能会重置它)想过重新实现具有此功能的Timer
类有多难,或者如何使用其中一个BCL类可靠地实现它?
发布于 2009-06-25 05:31:38
除了System.Threading.Timer之外,所有的计时器都具有等同于Start()和Stop()方法的功能。
所以像这样的扩展方法...
public static void Reset(this Timer timer)
{
timer.Stop();
timer.Start();
}
...is只有一种方法。
发布于 2012-11-22 21:04:04
我刚刚给计时器分配了一个新值:
mytimer.Change(10000,0);//重置为10秒
它对我来说很好。
在代码的顶部定义计时器:System.Threading.Timer myTimer;
if (!active)
myTimer = new Timer(new TimerCallback(TimerProc));
myTimer.Change(10000, 0);
active = true;
private void TimerProc(object state)
{
// The state object is the Timer object.
var t = (Timer)state;
t.Dispose();
Console.WriteLine("The timer callback executes.");
active = false;
// Action to do when timer is back to zero
}
发布于 2010-06-30 02:19:25
用于计时器(System.Windows.Forms.Timer)。
先是.Stop,然后是.Start方法,就像重置一样。
https://stackoverflow.com/questions/1042312
复制相似问题