开发者

Do something every N seconds in sync with the Windows system clock

开发者 https://www.devze.com 2023-03-22 07:34 出处:网络
There are plenty of tips on how to do something every N seconds, just nothing I have found that explicitly says whether any of these methods slowly drift out of synchronisation with the system clock d

There are plenty of tips on how to do something every N seconds, just nothing I have found that explicitly says whether any of these methods slowly drift out of synchronisation with the system clock due to latencies.

I have a multi-threaded application running on Windows. I need to know that within as few milliseconds as higher priority tasks and threads allow, one particular thread will reliably execute at "precisely" 1am according to the system clock, then at 1am + N seconds, 1am + 2N seconds, and so on... and keep doing this reliably through 11pm, 11pm + N, 11pm + 2N and so on indefinitely. Yes - it will be the case (for me anyway) that there are an integral number of these N second periods per hour.

I fully expect the actual time of thread execution to be 1am + d1, 1am + N + d2, 1am + 2N + d3, ... where each d(i) is some latency delta. That is all fine too, so long as the distribution of delta values remains reasonably 开发者_Go百科consistent and reasonably low - generally a few tens of milliseconds at most, with rare spikes above that. I do not mind if the system clock is out of sync with the global time standard.

What timing constructs can I use for this purpose? Is there something that maintains the synchronisation itself, or do I have to code something that will keep adjusting itself to maintain synchronisation as processor load rises and falls during the day?


Just look at the amount of time between now and the next time you should wake up, and sleep for that amount of time. So instead of setting a repeating timer to go off every n seconds, at each wakeup set a one-off timer, for however much time you need to elapse between now and the next point on the timeline.

Update: I've written a test program to see whether a repeating timer will drift.

The program starts at the beginning of the next minute, then wakes up every 5 seconds:

#include <windows.h>
#include<stdio.h>

ULONGLONG convertTime(SYSTEMTIME st) {
    FILETIME ft;
    ULARGE_INTEGER tm;

    SystemTimeToFileTime(&st, &ft);
    tm.u.LowPart = ft.dwLowDateTime;
    tm.u.HighPart = ft.dwHighDateTime;

    return tm.QuadPart;
}

ULONGLONG getNextMinute() {
    SYSTEMTIME now;
    GetSystemTime(&now);
    ULONGLONG ONE_MINUTE = 60 * 10000000;
    ULONGLONG nowNumeric = convertTime(now);
    ULONGLONG desiredTime = ((ULONGLONG)(nowNumeric / ONE_MINUTE)) * ONE_MINUTE + ONE_MINUTE;
    return desiredTime;
}

int main(int argc, char* argv[]) {
    ULONGLONG startTimeForTimer = getNextMinute();
    HANDLE hTimer = NULL;
    hTimer = CreateWaitableTimer(NULL, FALSE, NULL);
    if (NULL == hTimer) {
        printf("CreateWaitableTimer failed (%d)\n", GetLastError());
        return 1;
    }

    printf("Waiting for the next minute to start...\n");

    // Set a timer to wait for 5 seconds.
    if (!SetWaitableTimer(hTimer, &startTimeForTimer, 5000, NULL, NULL, 0))
    {
        printf("SetWaitableTimer failed (%d)\n", GetLastError());
        return 2;
    }

    // Wait for the timer.
    SYSTEMTIME beforeTime;
    SYSTEMTIME afterTime;

    GetSystemTime(&beforeTime);
    while (WaitForSingleObject(hTimer, INFINITE) == WAIT_OBJECT_0) {
        GetSystemTime(&afterTime);
        ULONGLONG elapsedTime = convertTime(afterTime) - convertTime(beforeTime);
        printf("Timer was signaled: elapsed=%I64u, seconds=%02d.%02d\n", elapsedTime, afterTime.wSecond, afterTime.wMilliseconds);
        GetSystemTime(&beforeTime);
    }

    return 0;
}

And here are the results (with some uninteresting bits removed) after running it for a while. The seconds= bit is the number of seconds (down to 1000th of a second) after the current minute:

Timer was signaled: elapsed=297650000, seconds=59.98
Timer was signaled: elapsed=50000000, seconds=04.986
Timer was signaled: elapsed=50000000, seconds=09.986
Timer was signaled: elapsed=50000000, seconds=14.986
Timer was signaled: elapsed=50000000, seconds=19.986
...
Timer was signaled: elapsed=50000000, seconds=09.986
Timer was signaled: elapsed=49990000, seconds=14.985
Timer was signaled: elapsed=50000000, seconds=19.985
...
Timer was signaled: elapsed=50000000, seconds=24.985
Timer was signaled: elapsed=49990000, seconds=29.984
...
Timer was signaled: elapsed=50000000, seconds=54.973
Timer was signaled: elapsed=50000000, seconds=59.973
Timer was signaled: elapsed=50000000, seconds=04.973
Timer was signaled: elapsed=49990000, seconds=09.972
Timer was signaled: elapsed=50000000, seconds=14.972
Timer was signaled: elapsed=50000000, seconds=19.972
Timer was signaled: elapsed=50000000, seconds=24.972
Timer was signaled: elapsed=50020000, seconds=29.974
Timer was signaled: elapsed=50020000, seconds=34.976
Timer was signaled: elapsed=50020000, seconds=39.978
Timer was signaled: elapsed=50020000, seconds=44.980
Timer was signaled: elapsed=50020000, seconds=49.982
Timer was signaled: elapsed=50020000, seconds=54.984
Timer was signaled: elapsed=50010000, seconds=59.985
Timer was signaled: elapsed=50020000, seconds=04.987
Timer was signaled: elapsed=50020000, seconds=09.989
Timer was signaled: elapsed=50020000, seconds=14.991
Timer was signaled: elapsed=50020000, seconds=19.993
Timer was signaled: elapsed=50020000, seconds=24.995
Timer was signaled: elapsed=50020000, seconds=29.997
Timer was signaled: elapsed=50020000, seconds=34.999
Timer was signaled: elapsed=50020000, seconds=40.01
Timer was signaled: elapsed=50020000, seconds=45.03
Timer was signaled: elapsed=50020000, seconds=50.05
Timer was signaled: elapsed=50020000, seconds=55.07
Timer was signaled: elapsed=50020000, seconds=00.09
Timer was signaled: elapsed=50010000, seconds=05.10
Timer was signaled: elapsed=50020000, seconds=10.12
Timer was signaled: elapsed=50020000, seconds=15.14
Timer was signaled: elapsed=50020000, seconds=20.16
Timer was signaled: elapsed=50020000, seconds=25.18
Timer was signaled: elapsed=50020000, seconds=30.20
Timer was signaled: elapsed=50020000, seconds=35.22
Timer was signaled: elapsed=50020000, seconds=40.24
Timer was signaled: elapsed=50020000, seconds=45.26
Timer was signaled: elapsed=50020000, seconds=50.28
Timer was signaled: elapsed=50020000, seconds=55.30
Timer was signaled: elapsed=50020000, seconds=00.32
Timer was signaled: elapsed=50010000, seconds=05.33
Timer was signaled: elapsed=50020000, seconds=10.35
Timer was signaled: elapsed=50020000, seconds=15.37
Timer was signaled: elapsed=50020000, seconds=20.39
Timer was signaled: elapsed=50020000, seconds=25.41
Timer was signaled: elapsed=50020000, seconds=30.43
Timer was signaled: elapsed=50020000, seconds=35.45
Timer was signaled: elapsed=50020000, seconds=40.47
Timer was signaled: elapsed=50020000, seconds=45.49
Timer was signaled: elapsed=50020000, seconds=50.51
Timer was signaled: elapsed=50020000, seconds=55.53
Timer was signaled: elapsed=50170000, seconds=00.70
Timer was signaled: elapsed=49870000, seconds=05.57
Timer was signaled: elapsed=50010000, seconds=10.58
Timer was signaled: elapsed=50020000, seconds=15.60
Timer was signaled: elapsed=50020000, seconds=20.62
Timer was signaled: elapsed=50020000, seconds=25.64
Timer was signaled: elapsed=50020000, seconds=30.66
Timer was signaled: elapsed=50020000, seconds=35.68
Timer was signaled: elapsed=50020000, seconds=40.70
Timer was signaled: elapsed=50020000, seconds=45.72
Timer was signaled: elapsed=50020000, seconds=50.74
Timer was signaled: elapsed=50020000, seconds=55.76
Timer was signaled: elapsed=50020000, seconds=00.78
Timer was signaled: elapsed=50020000, seconds=05.80
Timer was signaled: elapsed=50010000, seconds=10.81
Timer was signaled: elapsed=50020000, seconds=15.83
Timer was signaled: elapsed=50020000, seconds=20.85
Timer was signaled: elapsed=50020000, seconds=25.87
Timer was signaled: elapsed=50010000, seconds=30.88
Timer was signaled: elapsed=50010000, seconds=35.89
Timer was signaled: elapsed=50000000, seconds=40.89
Timer was signaled: elapsed=50010000, seconds=45.90
Timer was signaled: elapsed=50010000, seconds=50.91
Timer was signaled: elapsed=50000000, seconds=55.91
Timer was signaled: elapsed=50010000, seconds=00.92
Timer was signaled: elapsed=50010000, seconds=05.93
Timer was signaled: elapsed=50000000, seconds=10.93
Timer was signaled: elapsed=50010000, seconds=15.94
Timer was signaled: elapsed=50010000, seconds=20.95
Timer was signaled: elapsed=50000000, seconds=25.95
Timer was signaled: elapsed=50010000, seconds=30.96
Timer was signaled: elapsed=50010000, seconds=35.97
Timer was signaled: elapsed=50000000, seconds=40.97
Timer was signaled: elapsed=50010000, seconds=45.98
Timer was signaled: elapsed=50010000, seconds=50.99
Timer was signaled: elapsed=50000000, seconds=55.99
Timer was signaled: elapsed=50010000, seconds=00.100
Timer was signaled: elapsed=50010000, seconds=05.101
Timer was signaled: elapsed=50000000, seconds=10.101
Timer was signaled: elapsed=50010000, seconds=15.102
Timer was signaled: elapsed=50010000, seconds=20.103
Timer was signaled: elapsed=50010000, seconds=25.104
Timer was signaled: elapsed=50000000, seconds=30.104
Timer was signaled: elapsed=50010000, seconds=35.105
Timer was signaled: elapsed=50010000, seconds=40.106
Timer was signaled: elapsed=50000000, seconds=45.106
Timer was signaled: elapsed=50010000, seconds=50.107
Timer was signaled: elapsed=50010000, seconds=55.108
Timer was signaled: elapsed=50000000, seconds=00.108
Timer was signaled: elapsed=50010000, seconds=05.109
Timer was signaled: elapsed=50010000, seconds=10.110
Timer was signaled: elapsed=50000000, seconds=15.110
Timer was signaled: elapsed=50010000, seconds=20.111
Timer was signaled: elapsed=50010000, seconds=25.112
Timer was signaled: elapsed=50000000, seconds=30.112
Timer was signaled: elapsed=50010000, seconds=35.113
Timer was signaled: elapsed=50010000, seconds=40.114
Timer was signaled: elapsed=50000000, seconds=45.114
Timer was signaled: elapsed=50010000, seconds=50.115
Timer was signaled: elapsed=50010000, seconds=55.116
Timer was signaled: elapsed=50000000, seconds=00.116
Timer was signaled: elapsed=50010000, seconds=05.117
Timer was signaled: elapsed=50010000, seconds=10.118
Timer was signaled: elapsed=50000000, seconds=15.118
Timer was signaled: elapsed=50010000, seconds=20.119
Timer was signaled: elapsed=50010000, seconds=25.120
Timer was signaled: elapsed=50000000, seconds=30.120
Timer was signaled: elapsed=50010000, seconds=35.121
Timer was signaled: elapsed=50010000, seconds=40.122
Timer was signaled: elapsed=50000000, seconds=45.122
Timer was signaled: elapsed=50010000, seconds=50.123
Timer was signaled: elapsed=50010000, seconds=55.124
Timer was signaled: elapsed=50000000, seconds=00.124
Timer was signaled: elapsed=50010000, seconds=05.125
Timer was signaled: elapsed=50010000, seconds=10.126
Timer was signaled: elapsed=50000000, seconds=15.126
Timer was signaled: elapsed=50010000, seconds=20.127
Timer was signaled: elapsed=50010000, seconds=25.128
Timer was signaled: elapsed=50000000, seconds=30.128
Timer was signaled: elapsed=50000000, seconds=35.128
Timer was signaled: elapsed=50000000, seconds=40.128
Timer was signaled: elapsed=50010000, seconds=45.129
Timer was signaled: elapsed=50000000, seconds=50.129
Timer was signaled: elapsed=50000000, seconds=55.129
Timer was signaled: elapsed=50000000, seconds=00.129
Timer was signaled: elapsed=50000000, seconds=05.129
Timer was signaled: elapsed=50000000, seconds=10.129
Timer was signaled: elapsed=50010000, seconds=15.130
Timer was signaled: elapsed=50000000, seconds=20.130
Timer was signaled: elapsed=50000000, seconds=25.130
Timer was signaled: elapsed=50000000, seconds=30.130
Timer was signaled: elapsed=50000000, seconds=35.130
Timer was signaled: elapsed=50010000, seconds=40.131
Timer was signaled: elapsed=50000000, seconds=45.131
Timer was signaled: elapsed=50000000, seconds=50.131
Timer was signaled: elapsed=50000000, seconds=55.131
Timer was signaled: elapsed=50000000, seconds=00.131
Timer was signaled: elapsed=50010000, seconds=05.132
Timer was signaled: elapsed=50000000, seconds=10.132
Timer was signaled: elapsed=50000000, seconds=15.132
Timer was signaled: elapsed=50000000, seconds=20.132
Timer was signaled: elapsed=50000000, seconds=25.132
Timer was signaled: elapsed=50010000, seconds=30.133
Timer was signaled: elapsed=50000000, seconds=35.133
Timer was signaled: elapsed=50000000, seconds=40.133
Timer was signaled: elapsed=50000000, seconds=45.133
Timer was signaled: elapsed=50000000, seconds=50.133
Timer was signaled: elapsed=50010000, seconds=55.134
Timer was signaled: elapsed=50000000, seconds=00.134
Timer was signaled: elapsed=50000000, seconds=05.134
Timer was signaled: elapsed=50000000, seconds=10.134
Timer was signaled: elapsed=50000000, seconds=15.134
Timer was signaled: elapsed=50000000, seconds=20.134
Timer was signaled: elapsed=50010000, seconds=25.135
Timer was signaled: elapsed=50000000, seconds=30.135
Timer was signaled: elapsed=50000000, seconds=35.135
Timer was signaled: elapsed=50000000, seconds=40.135
Timer was signaled: elapsed=50000000, seconds=45.135
Timer was signaled: elapsed=50010000, seconds=50.136
Timer was signaled: elapsed=50000000, seconds=55.136
Timer was signaled: elapsed=50000000, seconds=00.136
Timer was signaled: elapsed=50000000, seconds=05.136
Timer was signaled: elapsed=50000000, seconds=10.136
Timer was signaled: elapsed=50010000, seconds=15.137
Timer was signaled: elapsed=50000000, seconds=20.137
Timer was signaled: elapsed=50000000, seconds=25.137
Timer was signaled: elapsed=50000000, seconds=30.137
Timer was signaled: elapsed=50000000, seconds=35.137
Timer was signaled: elapsed=50010000, seconds=40.138
Timer was signaled: elapsed=50000000, seconds=45.138
Timer was signaled: elapsed=50000000, seconds=50.138
Timer was signaled: elapsed=50000000, seconds=55.138
Timer was signaled: elapsed=50000000, seconds=00.138
Timer was signaled: elapsed=50010000, seconds=05.139
Timer was signaled: elapsed=50000000, seconds=10.139
Timer was signaled: elapsed=50000000, seconds=15.139
Timer was signaled: elapsed=50000000, seconds=20.139
Timer was signaled: elapsed=50000000, seconds=25.139
Timer was signaled: elapsed=50000000, seconds=30.139
Timer was signaled: elapsed=50000000, seconds=35.139
Timer was signaled: elapsed=50000000, seconds=40.139
Timer was signaled: elapsed=50000000, seconds=45.139
Timer was signaled: elapsed=50000000, seconds=50.139
Timer was signaled: elapsed=50000000, seconds=55.139
Timer was signaled: elapsed=50010000, seconds=00.140
Timer was signaled: elapsed=50000000, seconds=05.140
Timer was signaled: elapsed=50000000, seconds=10.140
Timer was signaled: elapsed=50000000, seconds=15.140
Timer was signaled: elapsed=50000000, seconds=20.140
Timer was signaled: elapsed=50000000, seconds=25.140
Timer was signaled: elapsed=50000000, seconds=30.140
Timer was signaled: elapsed=50000000, seconds=35.140
Timer was signaled: elapsed=50000000, seconds=40.140
Timer was signaled: elapsed=50000000, seconds=45.140
Timer was signaled: elapsed=50000000, seconds=50.140
Timer was signaled: elapsed=50000000, seconds=55.140
Timer was signaled: elapsed=50000000, seconds=00.140
Timer was signaled: elapsed=50000000, seconds=05.140
Timer was signaled: elapsed=50000000, seconds=10.140
Timer was signaled: elapsed=50000000, seconds=15.140
Timer was signaled: elapsed=50000000, seconds=20.140
Timer was signaled: elapsed=50000000, seconds=25.140
Timer was signaled: elapsed=50000000, seconds=30.140
Timer was signaled: elapsed=50000000, seconds=35.140
Timer was signaled: elapsed=50000000, seconds=40.140
Timer was signaled: elapsed=50000000, seconds=45.140
Timer was signaled: elapsed=50000000, seconds=50.140
Timer was signaled: elapsed=50000000, seconds=55.140
Timer was signaled: elapsed=50000000, seconds=00.140
Timer was signaled: elapsed=50000000, seconds=05.140
Timer was signaled: elapsed=50000000, seconds=10.140
Timer was signaled: elapsed=50000000, seconds=15.140
Timer was signaled: elapsed=50000000, seconds=20.140
Timer was signaled: elapsed=50000000, seconds=25.140
Timer was signaled: elapsed=50000000, seconds=30.140
Timer was signaled: elapsed=50010000, seconds=35.141
Timer was signaled: elapsed=50000000, seconds=40.141
Timer was signaled: elapsed=50000000, seconds=45.141
Timer was signaled: elapsed=50000000, seconds=50.141
Timer was signaled: elapsed=50000000, seconds=55.141
Timer was signaled: elapsed=50000000, seconds=00.141
Timer was signaled: elapsed=50000000, seconds=05.141
Timer was signaled: elapsed=50000000, seconds=10.141
Timer was signaled: elapsed=50000000, seconds=15.141
Timer was signaled: elapsed=50000000, seconds=20.141
Timer was signaled: elapsed=50000000, seconds=25.141
Timer was signaled: elapsed=50000000, seconds=30.141
Timer was signaled: elapsed=50000000, seconds=35.141
Timer was signaled: elapsed=50000000, seconds=40.141
Timer was signaled: elapsed=50000000, seconds=45.141
Timer was signaled: elapsed=50000000, seconds=50.141
Timer was signaled: elapsed=50000000, seconds=55.141
Timer was signaled: elapsed=50000000, seconds=00.141
Timer was signaled: elapsed=50000000, seconds=05.141
Timer was signaled: elapsed=50000000, seconds=10.141
Timer was signaled: elapsed=50000000, seconds=15.141
Timer was signaled: elapsed=50000000, seconds=20.141
Timer was signaled: elapsed=50000000, seconds=25.141
Timer was signaled: elapsed=50000000, seconds=30.141
Timer was signaled: elapsed=50000000, seconds=35.141
Timer was signaled: elapsed=50000000, seconds=40.141
Timer was signaled: elapsed=50000000, seconds=45.141
Timer was signaled: elapsed=50000000, seconds=50.141
Timer was signaled: elapsed=50000000, seconds=55.141
Timer was signaled: elapsed=50000000, seconds=00.141
Timer was signaled: elapsed=50000000, seconds=05.141
Timer was signaled: elapsed=50000000, seconds=10.141
Timer was signaled: elapsed=50000000, seconds=15.141
Timer was signaled: elapsed=50000000, seconds=20.141
Timer was signaled: elapsed=50000000, seconds=25.141
Timer was signaled: elapsed=50000000, seconds=30.141
Timer was signaled: elapsed=50000000, seconds=35.141
Timer was signaled: elapsed=50000000, seconds=40.141
Timer was signaled: elapsed=50000000, seconds=45.141
Timer was signaled: elapsed=50000000, seconds=50.141
Timer was signaled: elapsed=50000000, seconds=55.141
Timer was signaled: elapsed=50000000, seconds=00.141
Timer was signaled: elapsed=50000000, seconds=05.141
Timer was signaled: elapsed=50000000, seconds=10.141
Timer was signaled: elapsed=50000000, seconds=15.141
Timer was signaled: elapsed=49990000, seconds=20.140
Timer was signaled: elapsed=50000000, seconds=25.140
Timer was signaled: elapsed=50000000, seconds=30.140
Timer was signaled: elapsed=50000000, seconds=35.140
Timer was signaled: elapsed=50000000, seconds=40.140
Timer was signaled: elapsed=50000000, seconds=45.140
Timer was signaled: elapsed=50000000, seconds=50.140
Timer was signaled: elapsed=50000000, seconds=55.140
Timer was signaled: elapsed=50000000, seconds=00.140
Timer was signaled: elapsed=50000000, seconds=05.140
Timer was signaled: elapsed=50000000, seconds=10.140
Timer was signaled: elapsed=50000000, seconds=15.140
Timer was signaled: elapsed=50000000, seconds=20.140
Timer was signaled: elapsed=50000000, seconds=25.140
Timer was signaled: elapsed=50000000, seconds=30.140
Timer was signaled: elapsed=50000000, seconds=35.140
Timer was signaled: elapsed=50000000, seconds=40.140
Timer was signaled: elapsed=50000000, seconds=45.140
Timer was signaled: elapsed=50000000, seconds=50.140
Timer was signaled: elapsed=50000000, seconds=55.140
Timer was signaled: elapsed=50000000, seconds=00.140
Timer was signaled: elapsed=50000000, seconds=05.140
Timer was signaled: elapsed=50000000, seconds=10.140
Timer was signaled: elapsed=50000000, seconds=15.140
Timer was signaled: elapsed=50000000, seconds=20.140
Timer was signaled: elapsed=50000000, seconds=25.140
Timer was signaled: elapsed=50000000, seconds=30.140
Timer was signaled: elapsed=50000000, seconds=35.140
Timer was signaled: elapsed=50000000, seconds=40.140
Timer was signaled: elapsed=50000000, seconds=45.140
Timer was signaled: elapsed=50000000, seconds=50.140
Timer was signaled: elapsed=49990000, seconds=55.139
Timer was signaled: elapsed=50000000, seconds=00.139
Timer was signaled: elapsed=50000000, seconds=05.139
Timer was signaled: elapsed=50000000, seconds=10.139
Timer was signaled: elapsed=50000000, seconds=15.139
Timer was signaled: elapsed=50000000, seconds=20.139
Timer was signaled: elapsed=50000000, seconds=25.139
Timer was signaled: elapsed=50000000, seconds=30.139
Timer was signaled: elapsed=50000000, seconds=35.139
Timer was signaled: elapsed=50000000, seconds=40.139
Timer was signaled: elapsed=50000000, seconds=45.139
Timer was signaled: elapsed=50000000, seconds=50.139
Timer was signaled: elapsed=50000000, seconds=55.139
Timer was signaled: elapsed=50000000, seconds=00.139
Timer was signaled: elapsed=50000000, seconds=05.139
Timer was signaled: elapsed=50000000, seconds=10.139
Timer was signaled: elapsed=50000000, seconds=15.139
Timer was signaled: elapsed=50000000, seconds=20.139
Timer was signaled: elapsed=50000000, seconds=25.139
Timer was signaled: elapsed=49990000, seconds=30.138
Timer was signaled: elapsed=50000000, seconds=35.138
Timer was signaled: elapsed=50000000, seconds=40.138
Timer was signaled: elapsed=50000000, seconds=45.138
Timer was signaled: elapsed=50000000, seconds=50.138
Timer was signaled: elapsed=50000000, seconds=55.138
Timer was signaled: elapsed=50000000, seconds=00.138
Timer was signaled: elapsed=50000000, seconds=05.138
Timer was signaled: elapsed=50000000, seconds=10.138
Timer was signaled: elapsed=50000000, seconds=15.138
Timer was signaled: elapsed=50000000, seconds=20.138
Timer was signaled: elapsed=50000000, seconds=25.138
Timer was signaled: elapsed=50000000, seconds=30.138
Timer was signaled: elapsed=50000000, seconds=35.138
Timer was signaled: elapsed=50000000, seconds=40.138
Timer was signaled: elapsed=50000000, seconds=45.138
Timer was signaled: elapsed=49990000, seconds=50.137
Timer was signaled: elapsed=50000000, seconds=55.137
Timer was signaled: elapsed=50000000, seconds=00.137
Timer was signaled: elapsed=50000000, seconds=05.137
Timer was signaled: elapsed=50000000, seconds=10.137
Timer was signaled: elapsed=50000000, seconds=15.137
Timer was signaled: elapsed=50000000, seconds=20.137
Timer was signaled: elapsed=50000000, seconds=25.137
Timer was signaled: elapsed=50000000, seconds=30.137
Timer was signaled: elapsed=50000000, seconds=35.137
Timer was signaled: elapsed=50000000, seconds=40.137
Timer was signaled: elapsed=50000000, seconds=45.137
Timer was signaled: elapsed=50000000, seconds=50.137
Timer was signaled: elapsed=50000000, seconds=55.137
Timer was signaled: elapsed=50000000, seconds=00.137
Timer was signaled: elapsed=49990000, seconds=05.136
Timer was signaled: elapsed=50000000, seconds=10.136
Timer was signaled: elapsed=50000000, seconds=15.136
Timer was signaled: elapsed=50000000, seconds=20.136
Timer was signaled: elapsed=50000000, seconds=25.136
Timer was signaled: elapsed=50000000, seconds=30.136
Timer was signaled: elapsed=50000000, seconds=35.136
Timer was signaled: elapsed=50000000, seconds=40.136
Timer was signaled: elapsed=50000000, seconds=45.136
Timer was signaled: elapsed=50000000, seconds=50.136
Timer was signaled: elapsed=50000000, seconds=55.136
Timer was signaled: elapsed=50000000, seconds=00.136
Timer was signaled: elapsed=50000000, seconds=05.136
Timer was signaled: elapsed=50000000, seconds=10.136
Timer was signaled: elapsed=50000000, seconds=15.136
Timer was signaled: elapsed=50000000, seconds=20.136
Timer was signaled: elapsed=49990000, seconds=25.135
Timer was signaled: elapsed=50000000, seconds=30.135
Timer was signaled: elapsed=50000000, seconds=35.135
Timer was signaled: elapsed=50000000, seconds=40.135
Timer was signaled: elapsed=50000000, seconds=45.135
Timer was signaled: elapsed=50000000, seconds=50.135
Timer was signaled: elapsed=50000000, seconds=55.135
Timer was signaled: elapsed=50000000, seconds=00.135
Timer was signaled: elapsed=50000000, seconds=05.135
Timer was signaled: elapsed=50000000, seconds=10.135
Timer was signaled: elapsed=50000000, seconds=15.135
Timer was signaled: elapsed=50000000, seconds=20.135
Timer was signaled: elapsed=50000000, seconds=25.135
Timer was signaled: elapsed=50000000, seconds=30.135
Timer was signaled: elapsed=50000000, seconds=35.135
Timer was signaled: elapsed=50000000, seconds=40.135
Timer was signaled: elapsed=49990000, seconds=45.134
Timer was signaled: elapsed=50000000, seconds=50.134
Timer was signaled: elapsed=50000000, seconds=55.134
Timer was signaled: elapsed=50000000, seconds=00.134
Timer was signaled: elapsed=50000000, seconds=05.134
Timer was signaled: elapsed=50000000, seconds=10.134
Timer was signaled: elapsed=50000000, seconds=15.134
Timer was signaled: elapsed=50000000, seconds=20.134
Timer was signaled: elapsed=50000000, seconds=25.134
Timer was signaled: elapsed=50000000, seconds=30.134
Timer was signaled: elapsed=50000000, seconds=35.134
Timer was signaled: elapsed=50000000, seconds=40.134
Timer was signaled: elapsed=50000000, seconds=45.134
Timer was signaled: elapsed=50000000, seconds=50.134
Timer was signaled: elapsed=50000000, seconds=55.134
Timer was signaled: elapsed=49990000, seconds=00.133
Timer was signaled: elapsed=50000000, seconds=05.133
Timer was signaled: elapsed=50000000, seconds=10.133
Timer was signaled: elapsed=50000000, seconds=15.133
Timer was signaled: elapsed=50000000, seconds=20.133
Timer was signaled: elapsed=50000000, seconds=25.133
Timer was signaled: elapsed=50000000, seconds=30.133
Timer was signaled: elapsed=50000000, seconds=35.133
Timer was signaled: elapsed=50000000, seconds=40.133
Timer was signaled: elapsed=50000000, seconds=45.133
Timer was signaled: elapsed=50000000, seconds=50.133
Timer was signaled: elapsed=50000000, seconds=55.133
Timer was signaled: elapsed=50000000, seconds=00.133
Timer was signaled: elapsed=50000000, seconds=05.133
Timer was signaled: elapsed=50000000, seconds=10.133
Timer was signaled: elapsed=50000000, seconds=15.133
Timer was signaled: elapsed=49990000, seconds=20.132
Timer was signaled: elapsed=50000000, seconds=25.132
Timer was signaled: elapsed=50000000, seconds=30.132
Timer was signaled: elapsed=50000000, seconds=35.132
Timer was signaled: elapsed=50000000, seconds=40.132
Timer was signaled: elapsed=50000000, seconds=45.132
Timer was signaled: elapsed=50000000, seconds=50.132
Timer was signaled: elapsed=50000000, seconds=55.132
Timer was signaled: elapsed=50000000, seconds=00.132
Timer was signaled: elapsed=50000000, seconds=05.132
Timer was signaled: elapsed=50000000, seconds=10.132
Timer was signaled: elapsed=50000000, seconds=15.132
Timer was signaled: elapsed=50000000, seconds=20.132
Timer was signaled: elapsed=50000000, seconds=25.132
Timer was signaled: elapsed=50000000, seconds=30.132
Timer was signaled: elapsed=49990000, seconds=35.131
Timer was signaled: elapsed=50000000, seconds=40.131
Timer was signaled: elapsed=50000000, seconds=45.131
Timer was signaled: elapsed=50000000, seconds=50.131
Timer was signaled: elapsed=50000000, seconds=55.131
Timer was signaled: elapsed=50000000, seconds=00.131

Summary: Actually I don't know what to make of these results. It looks like Windows is trying to actively correct for any drift in the timer, so that every wakeup happens at the "appropriate" time. If that's the case, then this (setting a single timer for the interval that you want) is definitely the easiest way to accomplish what you want.


CreateWaitableTimer and SetWaitableTimer let you wait (WaitForSingleObject, or completion port, or anything that waits) rather than sleep.

Plus, you can give it a precise absolute time, or a relative time, whichever you want. If you want precisely N seconds after 1AM, give it an absolute time of 1AM+N seconds.

Plus, it even works if the computer goes into hibernate in between, if you specify that.


The SetWaitableTimer function allows you to specify a particular absolute time, and a period.

What Hans and Adam seem to be concerned about is drift due to processing time when using a one-off timer (i.e. if you wake up due to a timer, and set a new timer for "in N seconds", the actual interval is N seconds + wakeup latency + time to create new timer. By using a periodic timer, you won't have this problem. Or you can use one-off timers, and specify the next due time in absolute terms, which will also prevent drift.

Altogether you will need CreateWaitableTimer, SetWaitableTimer, and one of the wait functions, such as WaitForMultipleObjectsEx.

0

精彩评论

暂无评论...
验证码 换一张
取 消