【问题标题】:Creating an interrupt in c for windows operating system在 c 中为 Windows 操作系统创建中断
【发布时间】:2026-01-26 00:35:02
【问题描述】:
#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;
}

我创建了一个 Timer1 函数,它调用 TASK1 函数或 every2ms。我从主函数调用 timer1 函数。 我的问题:我将 timer1、timer2 和 timer3 放在 while 循环中,然后按预期调用 TASK 函数。如果我想停止这个,那是什么想法?我想为此创建一个中断吗?如果我删除了while循环,那么它不会每2ms、10ms等调用一次函数。如何为上述程序创建中断??

【问题讨论】:

  • 如果您删除了 while(1) 循环,那么您的 main() 函数将退出并且您的程序将在任何计时器回调发生之前终止。任意替换为“Hit any key to continue”代码。
  • 如果我删除了 while 循环,那么它只执行了 3 次。但我为每 2 毫秒、10 毫秒等创建了一个计时器。
  • 您当然需要删除 DeleteTimerQueue() 以获取计时器以重复进行回调。
  • 为什么不调用 every2ms , 10ms ??
  • 我删除了 DeleteTimerQueue() 函数然后它也不起作用。

标签: c windows timer scheduled-tasks interrupt


【解决方案1】:

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;
}

【讨论】:

  • 非常感谢您的回复。
  • @user2984410 欢迎您。如果它解决了您的问题,您可以将其标记为答案
  • 你能给我一个上面例子的软件中断的例子吗?我知道中断理论,但对编程感到困惑。
  • @user2984410 最好使用内核函数。加上大部分窗口的中断都没有记录。我从来没有在窗口中发现它们
  • 我使用了你的 main 函数和 HANDLE Timer Task 函数。我根据我的要求修改了回调。方法对吗??
最近更新 更多