2

I'm trying to update the UI periodically, System.Windows.Forms.Timer seems to be the obvious choice since it runs on the UI thread, however there's a note on MSDN page Timer Class says:

The Windows Forms Timer component is single-threaded, and is limited to an accuracy of 55 milliseconds. If you require a multithreaded timer with greater accuracy, use the Timer class in the System.Timers namespace.

This makes me think the possible interval values would be the multiples of 55, but it's not:

private Stopwatch _systemTimersTimerStopwatch = Stopwatch.StartNew();
private Stopwatch _windowsFormsTimerStopwatch = Stopwatch.StartNew();
private long _systemTimersTimerLastElapsed;
private long _windowsFormsTimerLastElapsed;

private void SystemTimersTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    //if (InvokeRequired) {
        BeginInvoke(new MethodInvoker(delegate {
            long elapsed = _systemTimersTimerStopwatch.ElapsedMilliseconds;
            if (_systemTimersTimerLastElapsed != 0) {
                Console.WriteLine($"+ System.Timers.Timer: {elapsed - _systemTimersTimerLastElapsed,2} ms");
            }
            _systemTimersTimerLastElapsed = _systemTimersTimerStopwatch.ElapsedMilliseconds;
        }));
    //}
}

private void WindowsFormsTimer_Tick(object sender, EventArgs e)
{
    long elapsed = _windowsFormsTimerStopwatch.ElapsedMilliseconds;
    if (_windowsFormsTimerLastElapsed != 0) {
        Console.WriteLine($"- Windows.Forms.Timer: {elapsed - _windowsFormsTimerLastElapsed,2} ms");
    }
    _windowsFormsTimerLastElapsed = _windowsFormsTimerStopwatch.ElapsedMilliseconds;
}

Interval properties of both are set to 1, outputs:

+ System.Timers.Timer: 15 ms
- Windows.Forms.Timer: 15 ms
+ System.Timers.Timer: 16 ms
- Windows.Forms.Timer: 20 ms
+ System.Timers.Timer: 15 ms
- Windows.Forms.Timer: 10 ms
+ System.Timers.Timer: 16 ms
- Windows.Forms.Timer: 18 ms
+ System.Timers.Timer: 16 ms
- Windows.Forms.Timer: 14 ms

Clearly System.Windows.Forms.timer is capable of accuracy lower than the "55 milliseconds" mentioned on MSDN page, where was this number even came from? There's another MSDN page titled Limitations of the Windows Forms Timer Component's Interval Property doesn't mention this "55 milliseconds" limit.

Also, is there any advantage of using a System.Timers.Timer over System.Windows.Forms.Timer for my use case? My guess is no since I'd need to use BeginInvoke anyway.

RadarNyan
  • 306
  • 1
  • 2
  • 12
  • 3
    55 is the olden DOS/Windows9x value. Windows NT runs by default 64 clock interrupts per second, 15.625 msec. Older versions of Windows may be affected by other processes calling timeBeginPeriod(), as is commonly done by browsers. So you may observe 10 msec, as low as 1 msec for Chrome. Low values are very detrimental to battery life. – Hans Passant Mar 23 '23 at 07:47
  • Comparison of various timers can be found at the end of the section: https://learn.microsoft.com/en-us/dotnet/api/system.timers.timer?view=net-7.0#remarks – shingo Mar 23 '23 at 07:57
  • @HansPassant Wow, I didn't even know winforms run on Windows 9X. So I guess it's safe to assume `Windows.Forms.Timer` would have the same accuracy as `System.Timers.Timer` on later systems, given the UI thread isn't busy of course? However I failed to observe sub 10 ms values even when Chrome is running (I've confirmed system time interval is set to 1 ms with clockres utility from Sysinternals) but I don't need that anyway. – RadarNyan Mar 23 '23 at 10:28
  • It seems [.Net timers does **not** respect system timer resolution](https://stackoverflow.com/a/52534595/2294234), that might be the reason why I didn't observe any lower interval. – RadarNyan Mar 23 '23 at 16:13

1 Answers1

-1

According to the documentation .NET framework clock can go down to "ticks" which is 100 nanoseconds or one ten-millionth of a second.

You can treat each tick as an event and every 100 nano seconds UI can be updated.

I've just built a form that uses the .NET framework timers in Windows forms. IT is loosing 8 minutes per day. I think they simply mean that the timer accuracy is within +\plus or minus 55 milliseconds per minute? or perhaps second?

The documentation is not clear.

mpAppProg
  • 1
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 25 '23 at 10:55