[WinAPI]如何进行计时器或中断? [英] [WinAPI] How to make a timer or Interrupt?
本文介绍了[WinAPI]如何进行计时器或中断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的所有人,
我想安排一个计时器来安排任务.我打算永远做一个循环,并通过GetLocalTime获取时间,如:
Dear all,
I want to make a timer to schedule a task. I intend make a loop forever and get time by GetLocalTime as:
while(1)
{
LPSYSTEMTIME time;
GetLocalTime(&time);
if(time.wHour == 10)
{
//do some tasks
}
}
但是,我意识到上面的代码将由于while(1)使得CPU总是很忙.因此,可以使用任何方法来计划执行指定任务,而无需检查"while(1)".例如:中断,当本地时间是10h时,有一个中断来唤醒我的代码吗?
谢谢您的意见.
However, I realize above code will make CPU always busy because of while(1). So there is any method to schedule to carry out specified task without "while(1)" to check. For ex: Interrupt, when local time is 10h, there is an interrupt to wake up my code?
Thank your for you comments
推荐答案
如果不使用OS的计时器功能,您将无法创建真正的计时器.计时器始终是OS的功能.它使用硬件和设备驱动程序,在等待计时器事件的同时,使用零CPU时间在所需时间触发一些事件/消息.应用程序级代码无法直接访问硬件和硬件中断,因此无论您做什么,它都没有用.
在Windows中,可以使用许多不同的计时器.有关更多信息,请参见此CodeProject文章: Timers教程 [
You cannot make a true timer without using timer features of OS. A timer is always an OS feature. It uses hardware and a device driver to trigger some event/message at desired time using zero CPU time while waiting for a timer event. Application-level code does not get direct access to hardware and hardware interrupts, so no matter what you do, it''s useless.
In Windows, there is a number of different timers you can use. For further information, please see this CodeProject article: Timers Tutorial[^].
—SA
这篇关于[WinAPI]如何进行计时器或中断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文