简体   繁体   English

在C#中使用计时器Tick计算毫秒

[英]calculate milliseconds with timer Tick in c#

I want to display milliseconds in label with Timer_Tick event. 我想在带有Timer_Tick事件的标签中显示毫秒。 But even if i initialize the Timer.interval to 1, I cant get the actual millisecond. 但是,即使我将Timer.interval初始化为1,我也无法获得实际的毫秒数。

Windows and C# are not created to be a realtime environment. Windows和C#并非创建为实时环境。 A timer is not guaranteed to trigger on the exact millisecond that is set in the properties. 不能保证计时器会在属性中设置的确切毫秒数上触发。

Aside from that, the typical LCD displays, nowadays run on 60 Hz. 除此之外,如今典型的LCD显示屏以60 Hz的频率运行。 So you get a refresh interval that is larger as 1 millisecond. 因此,您将获得一个大于1毫秒的刷新间隔。 This means you cannot display milliseconds in a label on a display that has a refresh rate smaller as 1000 Hz. 这意味着您不能在刷新率小于1000 Hz的显示器上的标签中显示毫秒。

Had you better explained what you wanted to do in your question, I think you would have received better answers! 如果您能更好地说明您想在问题中做什么,我想您会得到更好的答案!

So you want to create a stopwatch. 因此,您想创建一个秒表。

The way to do this is to use DateTime.Now , which returns the current date and time. 为此,可以使用DateTime.Now ,它返回当前的日期和时间。 When the Start button is clicked, you save DateTime.Now for later use: 单击“开始”按钮后,将保存DateTime.Now供以后使用:

private DateTime startTime;

private void StartButton_Click(...)
{
    startTime = DateTime.Now;
}

Then, every now and then, you need to refresh the time shown in your label. 然后,您不时地需要刷新标签中显示的时间。 You already have a Timer with an interval set to 1. This won't fire every millisecond; 您已经有一个Timer ,其时间间隔设置为1。这不会每毫秒触发一次。 the interval is approximate. 间隔是近似的。 But this is not a problem! 但这不是问题!

private void Timer_Tick(...)
{
    TimeSpan timeElapsed = DateTime.Now - startTime;
    timeLabel.Text = timeElapsed.TotalSeconds.ToString("0.000");
}

So you don't rely on how often the event is fired. 因此,您不必依赖于事件触发的频率。 You simply calculate how much time has elapsed since you started the timer! 您只需计算自启动计时器以来经过了多少时间!

There are many ways you can display the resulting timeElapsed value. 您可以通过多种方式显示结果timeElapsed值。 Check out the Hours , Minutes , Seconds and Milliseconds fields. 检查HoursMinutesSecondsMilliseconds字段。

If you want to implement a stop watch, then you might want to use the class System.Diagnostics.Stopwatch . 如果要实现秒表,则可能要使用System.Diagnostics.Stopwatch类。 Have a look at that page, it contains a sample program. 看一下该页面,其中包含一个示例程序。

The Stopwatch class has methods Start() , Stop() and Reset() to start/stop/reset measurement. Stopwatch类具有Start()Stop()Reset()来启动/停止/重置测量。 And you can use the Elapsed or ElapsedMilliseconds to get the time measured so far. 而且,您可以使用ElapsedElapsedMilliseconds来测量到目前为止的时间。

Regarding the resolution of the Stopwatch class, MSDN says this: 关于秒表类的分辨率,MSDN表示:

The Stopwatch measures elapsed time by counting timer ticks in the underlying timer mechanism. 秒表通过计算基础计时器机制中的计时器滴答来测量经过的时间。 If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time. 如果安装的硬件和操作系统支持高分辨率性能计数器,则Stopwatch类将使用该计数器来测量经过的时间。 Otherwise, the Stopwatch class uses the system timer to measure elapsed time. 否则,秒表类将使用系统计时器来测量经过的时间。 Use the Frequency and IsHighResolution fields to determine the precision and resolution of the Stopwatch timing implementation. 使用Frequency和IsHighResolution字段来确定秒表计时实现的精度和分辨率。

BTW: on my system the flag Stopwatch.IsHighResolution is set to true , and Stopwatch.Frequency returns a value of 3215342 (ticks per second) which is enough to measure microseconds. 顺便说一句:在我的系统上,标志Stopwatch.IsHighResolution设置为true ,而Stopwatch.Frequency返回值3215342 (每秒滴答数),足以测量微秒。

As others posted, you do not get a ms-exact time when waiting for timer events in Windows. 如其他人所述,在Windows中等待计时器事件时,您不会获得ms确切的时间。 You may however, measure the elapsed time between timer events very exactly. 但是,您可以非常精确地测量计时器事件之间的经过时间。 The usual way of doing this is to use the QueryPerformanceCounter API. 这样做的通常方法是使用QueryPerformanceCounter API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM