How to make an accurate decimal Timer?

后端 未结 2 1783
没有蜡笔的小新
没有蜡笔的小新 2020-12-12 03:19

I\'m pretty frustrated about this one ..

I have a timer called timer1 and a text box called TimeElapsedTextBox and a double va

相关标签:
2条回答
  • 2020-12-12 04:05

    Instead of using a timer, record the start time using DateTime.Now and then subtract the current time (DateTime.Now) from the start time. This will give you a more accurate timer as it uses the system clock instead which isn't affected so much by CPU performance.

    Alternatively you can use System.Diagnostics.Stopwatch which does this for you.

    You can still use an ordinary timer with an interval of less than a second to refresh the label displaying the time.

    0 讨论(0)
  • 2020-12-12 04:17

    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.

    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.

    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?

    Also, this is wrong:

    TimeTakenToFinish += (double)timer1.Interval / 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.

    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();
    }
    
    0 讨论(0)
提交回复
热议问题