C# Thread.Sleep(1000) takes much more than 1000ms on some pc

五迷三道 提交于 2019-12-02 10:22:34

Sleep will wait at least that long before activating the thread again, but it can always be longer than that. After the one second time has passed the thread becomes eligible to execute by the CPU scheduler, and the scheduler is able to run it whenever it wants to. If it's particularly busy, and/or is using a scheduling algorithm that doesn't focus on quickly allowing newly active threads to run, it could be some time.

Servy's answer is correct. For more details, please read the documentation:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms686298.aspx

which states:

Note that a ready thread is not guaranteed to run immediately. Consequently, the thread may not run until some time after the sleep interval elapses. For more information, see Scheduling Priorities.

The priority documentation is here:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms685100.aspx

More generally though: you are doing something deeply wrong. Do not ever use Sleep like this. If you want to wait some amount of time then use a timer or use a Delay task. Never sleep a thread like this outside of test code. The right way to write your code is probably something like

for (int countdown = 5; countdown > 0; countdown -= 1)
{
    label1.Text = countdown.ToString();
    await Task.Delay(1000);
}

Or, make a class that has a timer and a counter, start the timer to tick a few times a second, compare the current time to the time when you last updated the label, and if more than a second has elapsed, update the label.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!