Can I improve the resolution of Thread.Sleep?

前端 未结 5 1875
囚心锁ツ
囚心锁ツ 2020-12-07 01:51

Thread.Sleep() resolution varies from 1 to 15.6ms

Given this console app:

class Program
{
    static void Main()
    {
        int o         


        
5条回答
  •  感动是毒
    2020-12-07 02:24

    The answer is not to use Thread.Sleep and instead use a high resolution timer. You'll need to do your fade in a busy loop, but it sounds like that would be no problem. You simply cannot expect high resolution from Thread.Sleep and it is notorious for behaving differently on different hardware.

    You can use the Stopwatch class on .net which uses high-resolution performance counters if they are supported on the hardware.

提交回复
热议问题