C#秒表即时更新 [英] C# Stopwatch on-the-fly update

查看:110
本文介绍了C#秒表即时更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我的第一个问题在这里!) 我是C#的初学者.我试图建立一个简单的计时器(在Windows.Forms中). 我制作了一个指示时间的标签,并使用了StopWatch类(来自system.diagnostics).用于启动/停止秒表的触发事件是空格键KeyDown事件.第二次点击后,秒表停止,并将Label.text分配给Stopwatch.Elapsed值.我想不断更新标签,但我不知道如何. 如果我在事件本身中输入while(StopWatchName.IsRunning),则该事件将无限期继续,并且不会在第二次点击时响应.

(My first question here!) Hi, I am kind of beginner in c#. I tried to build a simple timer (in Windows.Forms). I made a label which indicates the time, and used the StopWatch class (from system.diagnostics). The trigger event for starting / stopping the stopwatch is the spacebar KeyDown event. After the second tap the stopwatch stops and Label.text is assigned to the Stopwatch.Elapsed value. I want to continuously update the label, but I don't know how. If I make while(StopWatchName.IsRunning) in the event itself, the event will indefinitely continue and won't respond for the second tap.

提前感谢任何想法!

推荐答案

您可能应该具有

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.

这篇关于C#秒表即时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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