如何制作一个准确的十进制计时器? [英] How to make an accurate decimal Timer?

查看:28
本文介绍了如何制作一个准确的十进制计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此感到非常沮丧..

I'm pretty frustrated about this one ..

我有一个名为 timer1 的计时器和一个名为 TimeElapsedTextBox 的文本框和一个名为 TimeTakenToFinishdouble 变量计时器每 1 秒(1000 毫秒)滴答一次在文本框中,我希望它以这种格式显示时间:

I have a timer called timer1 and a text box called TimeElapsedTextBox and a double variable called TimeTakenToFinish the timer ticks every 1 second (1000 millisecond) in the text box, I want it to display the time in this format:

Seconds.PartsOfSecond

这里是 Tick 事件:

Here is the Tick event:

private void timer1_Tick(object sender, EventArgs e)
{
  TimeTakenToFinish += (double)timer1.Interval / 10000;
  TimeElapsedTextBox.Text = TimeTakenToFinish;
}

它实际上是按照我想要的方式在文本框中显示它,但它没有正确计数..我的意思是,它只计算不到一秒..

it is actually displaying it in the text box the way i want it, but it's not counting properly .. I mean, it's counting less than a real second..

你能告诉我如何解决这个问题吗..

could you please tell me how to fix this ..

推荐答案

您的问题是对操作系统工作方式的误解.当然,您可以将间隔设置为 1000ms,但您不能期望它实际上每秒滴答一次.您在 Windows 上运行代码,而不是硬(或软)实时操作系统.

Your problem here is a misunderstanding of the way your OS works. Sure, you can set the interval to 1000ms, but you cannot expect it to actually tick every second. You are running code on Windows, not a hard (or soft) real time operating system.

顺便说一句,您还应该知道您的计时器的分辨率是有限的,截至今天,仅限于您的系统计时器的准确度,可能约为 15 毫秒.

As an aside, you should also know that the resolution of your timer is finite, and as of today, limited to the accuracy of your system timer, which is probably about 15ms.

您不能期望您的代码在那种环境中确定性地执行该操作.在任何时候,操作系统都可以抢先将您踢出 CPU 并开始处理另一项任务.

You cannot expect your code to perform that deterministically in that sort of environment. At any point the OS can preemptively kick you out of the CPU and start working on another task.

尽管我会问,但您根本无法获得所需的准确性;它实际上是必需的吗?可能不是,但你还没有告诉我们你在这里真正想完成什么,所以谁知道?

You simply cannot get the accuracy you desire, though I would ask; is it actually required? Probably not, but you haven't told us what you are actually trying to accomplish here, so who knows?

另外,这是错误的:

TimeTakenToFinish += (double)timer1.Interval / 10000;

Interval 是一个属性,用于告诉计时器大概多久触发一次 Tick 事件.您实际上并没有测量任何东西,您也可能只是每次都将 1000.0/10000 添加到您的计数器中.

Interval is a property which is used to tell the timer roughly how often it should fire the Tick event. You are not actually measuring anything, you may as well just be adding 1000.0 / 10000 to your counter every time.

如果您需要更高的精度,请使用 StopWatch 类,该类使用 CPU 的高性能计时器(如果可用).您仍然可以使用计时器根据秒表的当前经过值定期更新 UI,即,

If you need more precision use the StopWatch class which uses your CPU's high performance timer if available. You can still use a timer to periodically update the UI based on the current elapsed value of the Stopwatch, i.e.,

void timer1_Tick(...)
{
    var totalSeconds = _someStopwatch.ElapsedMilliseconds / 1000.0;
    TimeElapsedTextBox.Text = totalSeconds.ToString();
}

这篇关于如何制作一个准确的十进制计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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