Windows 8(.1)睡眠比所需时间多1毫秒

德米翁

SleepWindows 8.1x64上,持续时间总是比所需时间多1毫秒。例如,Sleep(1)持续时间大约为2毫秒,Sleep(2)-3等timeBeginPeriod设置为1。OnWindows 7可以正常工作(没有多余的毫秒)。此行为是否正常/是否可以解决?

#include <Windows.h>
#include <stdio.h>

#pragma comment(lib, "winmm.lib")

LARGE_INTEGER Frequency;

long long int GetCurrent()
{
    LARGE_INTEGER counter;

    QueryPerformanceCounter(&counter);

    return (1000000 * counter.QuadPart / Frequency.QuadPart);
}

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    timeBeginPeriod(1);

    QueryPerformanceFrequency(&Frequency);

    const unsigned int count = 1000;

    long long int buffer[count];

    long long int lastTime = GetCurrent(), currentTime;

    for (unsigned int i = 0; i < count; i++)
    {
        currentTime = GetCurrent();

        buffer[i] = currentTime - lastTime;

        lastTime = currentTime;

        Sleep(1);
    }

    timeEndPeriod(1);

    FILE *file = fopen("log.txt", "w");

    for (unsigned int i = 0; i < count; i++)
        fprintf(file, "%ld\n", buffer[i]);

    fclose(file);

    return EXIT_SUCCESS;
}
德米翁

NtDelayExecution解决方法多亏了Mehrdad

static NTSTATUS (__stdcall *NtDelayExecution)(BOOL Alertable, PLARGE_INTEGER DelayInterval) = (NTSTATUS (__stdcall*)(BOOL, PLARGE_INTEGER)) GetProcAddress(GetModuleHandle(L"ntdll.dll"), "NtDelayExecution");

LARGE_INTEGER delay;

unsigned int milliseconds = 1;

delay.QuadPart = (milliseconds > 1) ? -10000LL * (milliseconds - 1) : -1LL;

NtDelayExecution(false, &delay);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Windows中以C ++实现小于1毫秒的延迟

来自分类Dev

Java thread.sleep(1)睡眠时间超过1毫秒

来自分类Dev

Windows 8中的睡眠模式

来自分类Dev

毫秒时间戳=> ISO日期关闭了1毫秒

来自分类Dev

时代1/2103 /未知-8秒80毫秒/步-损失:0.0175(即使超过训练图像总数后,model.fit()仍会永远运行)

来自分类Dev

Windows 8上的“睡眠”按钮在哪里?

来自分类Dev

Windows 8. 1更新占78%?

来自分类Dev

为1秒钟的Windows Phone生成窦音8

来自分类Dev

Windows Phone 8试用到期时间

来自分类Dev

Thread.Sleep(1)花费的时间超过1毫秒

来自分类Dev

Thread.Sleep(1)花费的时间超过1毫秒

来自分类Dev

Javascript setInterval的执行时间不到1毫秒?

来自分类Dev

提高查询性能,使其在不到1毫秒的时间内运行/记录

来自分类Dev

Javascript setInterval的执行时间不到1毫秒?

来自分类Dev

Windows 8中没有混合睡眠吗?

来自分类Dev

Windows 8,1上的Microsoft Band SDK

来自分类Dev

使用xmlsec1验证Windows 8的Inapp购买收据

来自分类Dev

如何确定Windows Phone 8(.1)上的蓝牙是否打开

来自分类Dev

Windows有时会挂起300-600毫秒-由Performance Counter测量

来自分类Dev

Windows 10开机时间:以前是20秒,现在是6-8倍

来自分类Dev

.NET ReaderWriterLock的“ -1毫秒”有何意义?

来自分类Dev

Windows 8和Windows 10 Lockscreen在仅1分钟后超时,并且屏幕变黑

来自分类Dev

问题:Ubuntu和Windows 8之间的时间不同步

来自分类Dev

Windows 8是否支持UTC作为BIOS时间?

来自分类Dev

Windows 8是否支持UTC作为BIOS时间?

来自分类Dev

问题:Ubuntu和Windows 8之间的时间不同步

来自分类Dev

QSystemTrayIcon and Windows8

来自分类Dev

Windows 8 CLI截图

来自分类Dev

Windows Phone 8记录

Related 相关文章

  1. 1

    在Windows中以C ++实现小于1毫秒的延迟

  2. 2

    Java thread.sleep(1)睡眠时间超过1毫秒

  3. 3

    Windows 8中的睡眠模式

  4. 4

    毫秒时间戳=> ISO日期关闭了1毫秒

  5. 5

    时代1/2103 /未知-8秒80毫秒/步-损失:0.0175(即使超过训练图像总数后,model.fit()仍会永远运行)

  6. 6

    Windows 8上的“睡眠”按钮在哪里?

  7. 7

    Windows 8. 1更新占78%?

  8. 8

    为1秒钟的Windows Phone生成窦音8

  9. 9

    Windows Phone 8试用到期时间

  10. 10

    Thread.Sleep(1)花费的时间超过1毫秒

  11. 11

    Thread.Sleep(1)花费的时间超过1毫秒

  12. 12

    Javascript setInterval的执行时间不到1毫秒?

  13. 13

    提高查询性能,使其在不到1毫秒的时间内运行/记录

  14. 14

    Javascript setInterval的执行时间不到1毫秒?

  15. 15

    Windows 8中没有混合睡眠吗?

  16. 16

    Windows 8,1上的Microsoft Band SDK

  17. 17

    使用xmlsec1验证Windows 8的Inapp购买收据

  18. 18

    如何确定Windows Phone 8(.1)上的蓝牙是否打开

  19. 19

    Windows有时会挂起300-600毫秒-由Performance Counter测量

  20. 20

    Windows 10开机时间:以前是20秒,现在是6-8倍

  21. 21

    .NET ReaderWriterLock的“ -1毫秒”有何意义?

  22. 22

    Windows 8和Windows 10 Lockscreen在仅1分钟后超时,并且屏幕变黑

  23. 23

    问题:Ubuntu和Windows 8之间的时间不同步

  24. 24

    Windows 8是否支持UTC作为BIOS时间?

  25. 25

    Windows 8是否支持UTC作为BIOS时间?

  26. 26

    问题:Ubuntu和Windows 8之间的时间不同步

  27. 27

    QSystemTrayIcon and Windows8

  28. 28

    Windows 8 CLI截图

  29. 29

    Windows Phone 8记录

热门标签

归档