问题描述
Windows Server 2016/2019有问题在典型的循环中,用于"重复1000次,暂停时间为1毫秒.
There is a problem with Windows Server 2016/2019In a typical cycle "for" 1000 repetitions, with a pause of 1 ms.
for (int i = 0; i <1000; i ++)
{
Thread.Sleep (1);
}
服务器处理每个睡眠周期-15毫秒.并自1000年以来.而不是1000毫秒.(1秒)执行15000 ms.(15秒)
The server processes each Sleep cycle - 15 ms. And since 1000 requests.Instead of 1000 ms. (1 sec) 15000 ms is executed. (15 sec)
在家用PC上可以.仅在服务器上存在此问题.我怀疑是CPU的罪魁祸首.
On the home PC is all right. Only on the server is this problem.I suspect that the CPU is to blame.
推荐答案
一般来说:仅保证在指定时间内睡眠至少 时间-不是确切时间.您将获得下一个用于处理的timslot-最简单的解决方案-不要旋转和睡眠-如果您想要的话,睡眠1000毫秒-这样您就可以接近1睡眠.通过循环运行1000次,您的稍大一点的睡眠"就可以总结.
Genrally: sleep only gurantees to sleep at minimum for the specified time - not the exact time. You get the next timseslot for processing - easiest solution - dont spin and sleep - sleep 1000ms if you want to have that - so you'll get closer to 1s sleeps. By looping 1000 times your "slightly bigger sleeps" sum up.
请参见此处的C#: sleep()-更一般的Windows睡眠在这里: win32/api/synchapi/nf-synchapi-sleep .
See f.e. for C# here: sleep() - and more generally for Windows sleep here: win32/api/synchapi/nf-synchapi-sleep.
来源:
这篇关于Sleep()睡得太久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!