2013-12-21 44 views
1
#include <stdio.h> 
#include <windows.h> 
.. 

int main() 
{ 

while(1) 
{ 
Timer1(); // Timer1 calls the function called TASK1 for every2ms (CreateTimerQueueTimer) 
Timer2(); // Timer2 calls the function called TASK2 for every10ms 
Timer3(); // Timer3 calls the function called TASK3 for every100ms 
} 
return 0: 
} 


int Timer1() // This is only a Timer1 code 
{ 

    int arg; 
    HANDLE Task1; 
    HANDLE HTimer1 =NULL; 
    HANDLE HTimerQueue1 = NULL; 
    Task1 = CreateEvent(NULL, TRUE, FALSE, NULL); 
    if(NULL == Task1) 
    { 
     printf("CreateEvent failed (%d)\n", GetLastError()); 
     return 1; 
    } 

    //create a timer queue 
    HTimerQueue1 = CreateTimerQueue(); 
    if(NULL == HTimerQueue1) 
    { 
     printf("CreateTimerQueue failed (%d)\n", GetLastError()); 
     return 2; 
    } 

    //phNewTimer - Pointer to a handle; this is an out value 
    //TimerQueue - Timer queue handle. For the default timer queue, NULL 
    //Callback - Pointer to the callback function 
    //Parameter - Value passed to the callback function 
    //DueTime - Time (milliseconds), before the timer is set to the signaled state for the first time 
    //Period - Timer period (milliseconds). If zero, timer is signaled only once 
    //Flags - One or more of the next values (table taken from MSDN): 

    //set the timer to call the timer routine in 2ms 
    if(!CreateTimerQueueTimer(&HTimer1, HTimerQueue1, (WAITORTIMERCALLBACK)TASK1, &arg, 0,2,0)) 
    { 
     printf("CreateTimerQueueTimer failed (%d)\n", GetLastError()); 
     return 3; 
    } 


    //Delete all timers in the timer queue 
    if(!DeleteTimerQueue(HTimerQueue1)) 
     printf("DeleteTimerQueue failed (%d)\n", GetLastError()); 

    return 0; 
} 

我創建了一個調用TASK1函數或每個2ms的Timer1函數。我正在從主函數調用timer1函數。 我的問題:我把timer1,timer2和timer3放在while循環中,然後按預期調用TASK函數。如果我想停止這個,那麼這個想法是什麼?我想爲此創建一箇中斷嗎?如果我刪除while循環,那麼它不會每2ms,10ms等等調用函數。如何爲上述程序創建一箇中斷?在c中爲windows操作系統創建一箇中斷

+0

如果刪除,而(1)循環,然後你的main()函數將退出,你的程序將終止,可能會發生任何計時器回調之前。隨意用「按任意鍵繼續」代碼替換它。 –

+0

如果我刪除while循環,那麼它只執行三次。但是我爲每2ms,10ms等創建一個計時器。 – user2984410

+1

您當然需要刪除DeleteTimerQueue()以使定時器重複進行回調。 –

回答

0

Msdn 刪除DeleteTimerQueueTimer。而作爲評論指出做到這一點while循環 的外面,而不是寫三個時間功能,您可以編寫一個帶參數。

下面是簡單的非完成的示例

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

VOID CALLBACK task1(PVOID lpParam, BOOLEAN TimerOrWaitFired) 
{ 
    int* arg=(int*)lpParam; 
    printf("task1 we are called with arg %d\n",*arg); 

} 

VOID CALLBACK task2(PVOID lpParam, BOOLEAN TimerOrWaitFired) 
{ 
int* arg=(int*)lpParam; 
    printf("task2 we are called with arg %d\n",*arg); 
} 

VOID CALLBACK task3(PVOID lpParam, BOOLEAN TimerOrWaitFired) 
{ 
int* arg=(int*)lpParam; 
    printf("task3 we are called with arg %d\n",*arg); 
} 

HANDLE TimerTask(unsigned int period,WAITORTIMERCALLBACK task,void* arg) ; 
int main() 
{ 
    int arg1=1,arg2=2,arg3=3; 

HANDLE h1=TimerTask(2,task1,&arg1) ; 
HANDLE h2=TimerTask(10,task2,&arg2) ; 
HANDLE h3=TimerTask(8,task3,&arg3) ; 

getchar(); 
return 0; 
} 

HANDLE TimerTask(unsigned int period,WAITORTIMERCALLBACK task,void* arg) 
{ 

    HANDLE HTimer =NULL; 

    //set the timer to call the timer routine in 2ms 
    if(!CreateTimerQueueTimer(&HTimer, NULL, (WAITORTIMERCALLBACK)task,(PVOID) arg, 0,period*1000,0)) 
    { 
     printf("CreateTimerQueueTimer failed (%d)\n", GetLastError()); 
     return NULL; 
    } 


    return HTimer; 
} 
+0

非常感謝您的回覆。 – user2984410

+0

@ user2984410歡迎您。如果它解決了你的問題,你可以將它標記爲答案 – qwr

+0

你能給我一個上面例子的軟件中斷的例子嗎?我瞭解中斷的理論,但卻在編程方面感到困惑。 – user2984410

相關問題