Getting accurate ticks from a timer in C#

前端 未结 6 917
北荒
北荒 2020-12-28 18:21

I\'m trying to rebuild an old metronome application that was originally written using MFC in C++ to be written in .NET using C#. One of the issues I\'m running into is gett

6条回答
  •  执念已碎
    2020-12-28 18:48

    Timer classes can start behaving strangely when the timer 'tick' event code is not finished executing by the time the next 'tick' occurs. One way to combat this is to disable the timer at the beginning of the tick event, then re-enable it at the end.

    However, this approach is not suitable in cases where the execution time of the 'tick' code is not acceptable error in the timing of the tick, since the timer will be disabled (not counting) during that time.

    If disabling the timer is an option, then you can also achieve the same effect by creating a separate thread that executes, sleeps for x milliseconds, executes, sleeps, etc...

提交回复
热议问题