System.Timer elapsed event seems to fire late for short intervals in .Net

前端 未结 3 380
故里飘歌
故里飘歌 2021-01-15 14:22

I am sampling a system over the UART port, and then logging the info in a file with a timestamp (including milliseconds). If I sample at 1 second intervals, the data comes

3条回答
  •  [愿得一人]
    2021-01-15 15:10

    The .NET timers don't have infinite resolution. My experiments with .NET 3.5 and .NET 4.0 show that you can expect, at best, 15 ms resolution with the .NET timer objects. My experience has been that with a timer interval of 100 ms, you can expect a tick typically in the 100 to 105 ms range, but I've seen a range as wide as 99 to 120 ms.

    For full details see http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=815.

    If you need better accuracy, you can use the Windows timers directly. I created a wrapper for the Timer Queue API at http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=817.

提交回复
热议问题