C# Stopwatch on-the-fly update

不羁的心 提交于 2019-12-02 03:28:18

You should probably have a timer which fires frequently (e.g. every 10ms) - start the timer when you start the stopwatch, and stop the timer when you stop the stopwatch. The timer tick event would just set the label's Text property from the stopwatch.

The timer's interval won't be exact of course - but that's okay, because the point is to rely on the stopwatch for the actual timing. The timer is just there to update the label frequently.

You are probably going to want to use System.Timers. Timer class in order to call a function every few seconds to update your UI with the time elapased value.

Here is a good Sample: http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx

Basically, your OnTimedEvent event function from the sample is what will accomplish thisin your code.

EDIT: John is correct (see comments) you should be using Forms.Timer you can avoid thread marshaling. http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

TimerEventProcessor would be the function of concern in that sample.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!