#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 函数或每 2 毫秒调用一次的 Timer1 函数。我从主函数调用 timer1 函数。 我的问题:我将 timer1、timer2 和 timer3 放在 while 循环中,然后按预期调用 TASK 函数。如果我想阻止这个,那我的想法是什么??我想为此创建一个中断吗?如果我删除 while 循环,那么它不会每 2 毫秒、10 毫秒等调用一次该函数。如何为上述程序创建中断??
最佳答案
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;
}
关于在 c 中为 Windows 操作系统创建中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20718762/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法
这似乎非常适得其反,因为太多的gem会在window上破裂。我一直在处理很多mysql和ruby-mysqlgem问题(gem本身发生段错误,一个名为UnixSocket的类显然在Windows机器上不能正常工作,等等)。我只是在浪费时间吗?我应该转向不同的脚本语言吗? 最佳答案 我在Windows上使用Ruby的经验很少,但是当我开始使用Ruby时,我是在Windows上,我的总体印象是它不是Windows原生系统。因此,在主要使用Windows多年之后,开始使用Ruby促使我切换回原来的系统Unix,这次是Linux。Rub
我正在玩HTML5视频并且在ERB中有以下片段:mp4视频从在我的开发环境中运行的服务器很好地流式传输到chrome。然而firefox显示带有海报图像的视频播放器,但带有一个大X。问题似乎是mongrel不确定ogv扩展的mime类型,并且只返回text/plain,如curl所示:$curl-Ihttp://0.0.0.0:3000/pr6.ogvHTTP/1.1200OKConnection:closeDate:Mon,19Apr201012:33:50GMTLast-Modified:Sun,18Apr201012:46:07GMTContent-Type:text/plain