#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操作系统创建一个中断
如果删除,而(1)循环,然后你的main()函数将退出,你的程序将终止,可能会发生任何计时器回调之前。随意用“按任意键继续”代码替换它。 –
如果我删除while循环,那么它只执行三次。但是我为每2ms,10ms等创建一个计时器。 – user2984410
您当然需要删除DeleteTimerQueue()以使定时器重复进行回调。 –