2014-05-13 50 views
2

我想用CreateTimerQueueTimer(...)來經常運行一個函數。用CreateTimerQueueTimer,Visual Studio 2012,C++創建一個定時器,定期運行

我使用從MSDN的例子,主要是該行關注我:

CreateTimerQueueTimer(&hTimer, hTimerQueue,(WAITORTIMERCALLBACK)TimerRoutine, &arg , 50,100, 0) 

其語法是:

BOOL WINAPI CreateTimerQueueTimer(
    _Out_  PHANDLE phNewTimer, 
    _In_opt_ HANDLE TimerQueue, 
    _In_  WAITORTIMERCALLBACK Callback, 
    _In_opt_ PVOID Parameter, 
    _In_  DWORD DueTime, 
    _In_  DWORD Period, 
    _In_  ULONG Flags 
); 

倒數第二個參數狀態

期[in]

t的週期他計時器,以毫秒爲單位。如果此參數爲零,則定時器發信號一次。如果此參數大於零,則定時器是週期性的。週期性計時器每次經過一段時間後自動重新激活,直到計時器被取消。

正如你可以在我的代碼中看到的,我將50的期限和期限設置爲100.當我運行它時,它不會啓動repeat觸發定時器。有人可以幫我弄這個嗎 ?

這裏是整個代碼:

#include "stdafx.h" 
#include <string> 
#include <iostream> 
#include <thread> 
#include <Windows.h> 
#include <stdio.h> 

using namespace std; 

HANDLE gDoneEvent; 

VOID CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired) 
{ 
    if (lpParam == NULL) 
    { 
     printf("TimerRoutine lpParam is NULL\n"); 
    } 
    else 
    { 
     // lpParam points to the argument; in this case it is an int 

     printf("Timer routine called. Parameter is %d.\n", 
       *(int*)lpParam); 
     if(TimerOrWaitFired) 
     { 
      printf("The wait timed out.\n"); 
     } 
     else 
     { 
      printf("The wait event was signaled.\n"); 
     } 
    } 

    SetEvent(gDoneEvent); 
} 

int main() 
{ 
    HANDLE hTimer = NULL; 
    HANDLE hTimerQueue = NULL; 
    int arg = 123,x; 


    // Use an event object to track the TimerRoutine execution 
    gDoneEvent = CreateEvent(NULL, TRUE, FALSE, NULL); 
    if (NULL == gDoneEvent) 
    { 
     printf("CreateEvent failed (%d)\n", GetLastError()); 
     return 1; 
    } 

    // Create the timer queue. 
    hTimerQueue = CreateTimerQueue(); 
    if (NULL == hTimerQueue) 
    { 
     printf("CreateTimerQueue failed (%d)\n", GetLastError()); 
     return 2; 
    } 

    // Set a timer to call the timer routine in 10 seconds. 
    if (!CreateTimerQueueTimer(&hTimer, hTimerQueue,(WAITORTIMERCALLBACK)TimerRoutine, &arg , 50,100, 0)) 
    { 
     printf("CreateTimerQueueTimer failed (%d)\n", GetLastError()); 
     return 3; 
    } 

    // TODO: Do other useful work here 

    printf("Call timer routine in 10 seconds...\n"); 

    // Wait for the timer-queue thread to complete using an event 
    // object. The thread will signal the event at that time. 

    if (WaitForSingleObject(gDoneEvent, INFINITE) != WAIT_OBJECT_0) 
     printf("WaitForSingleObject failed (%d)\n", GetLastError()); 

    CloseHandle(gDoneEvent); 

    // Delete all timers in the timer queue. 
    if (!DeleteTimerQueue(hTimerQueue)) 
     printf("DeleteTimerQueue failed (%d)\n", GetLastError()); 
    cin>>x; 
    return 0; 
} 

謝謝

+0

http://www.sscce.org/ –

+0

您的代碼適用於我。你期望的輸出是什麼? – Sunius

+0

'通話計時器例程在10秒內... 計時器例程被調用。參數是123. 等待超時' 然後它等待我的鍵盤命令。 – user3463936

回答

0

事件gDoneEvent功能TimerRoutine()退出之前發出信號。對於重複調用回調函數,在功能TimerRoutine()被調用所需的次數後,發出事件gDoneEvent

可能包含以下幾行代碼。

static int count = 0; 

VOID CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired) 
{ 
    ...... 
    count++; 
    if(count == 100) 
    { 
     SetEvent(gDoneEvent); 
    } 
} 
0

@Lokesh是對的,雖然作爲一個C++初學者,我一開始並沒有得到他們的答案。

您需要確保SetEvent(gDoneEvent);僅在您希望定時器停止時纔會調用。爲了滿足我的需求,我只是將它評論出來,因爲我希望計時器能夠連續運行。

相關問題