Condividi tramite


Uso di oggetti timer waitable

Nell'esempio seguente viene creato un timer che verrà segnalato dopo un ritardo di 10 secondi. Innanzitutto, il codice usa la funzione CreateWaitableTimer per creare un oggetto timer waitable. Usa quindi la funzione SetWaitableTimer per impostare il timer. Il codice usa la funzione WaitForSingleObject per determinare quando il timer è stato segnalato.

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

int main()
{
    HANDLE hTimer = NULL;
    LARGE_INTEGER liDueTime;

    liDueTime.QuadPart = -100000000LL;

    // Create an unnamed waitable timer.
    hTimer = CreateWaitableTimer(NULL, TRUE, NULL);
    if (NULL == hTimer)
    {
        printf("CreateWaitableTimer failed (%d)\n", GetLastError());
        return 1;
    }

    printf("Waiting for 10 seconds...\n");

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

    // Wait for the timer.

    if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
        printf("WaitForSingleObject failed (%d)\n", GetLastError());
    else printf("Timer was signaled.\n");

    return 0;
}

Uso di timer waitable con una chiamata di procedura asincrona