线程而不是计时器 [英] threads instead of timers

查看:109
本文介绍了线程而不是计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这很简单,但是以某种方式我无法让Google帮助我.
我想要一个每秒触发一个事件的计时器操作.我想使用线程和waitonsingleobject或其他方法来执行此操作,而不是使用system :: timers :: timer.
谁能指导我该怎么做.
在此先感谢.

I know this is straight forward, but somehow I couldn''t get google to help me.
I want a timer operation that fires a event every second. I want to do this using threads and waitonsingleobject or something else instead of using system::timers::timer.
can anyone direct me how to do this.
Thanks in advance.

推荐答案

不确定为什么要这么做,但是...要做类似的事情(更像MFC,但我确定您可以算出一个等效值)...
1)创建线程
2)在线程内
-while()围绕睡眠/信号循环
-Sleep()一段时间
-睡眠完成后,用PostMessage()发出信号到您要提醒计时器的任何窗口
3)执行后,停止线程(正常运行!否则,您将在那里泄漏任何内存)

不知道为什么要使用WaitForSingleObject(),除非您试图阻止正在等待计时器的线程...在这种情况下,为什么要在其他线程上使用计时器?

...很抱歉缺少代码,准备去体育馆!干杯! :)
Not sure why you''d do this but... do something like this (more MFC-like, but I''m sure you can figure an equivalent)...
1) Create Thread
2) Within thread
-while() loop around a sleep/signal loop
-Sleep() for a period of time
-when done with sleep, signal with a PostMessage() to whatever window you want to alert of the timer
3) After execution, stop thread (gracefully!, or else you''ll leak any memory there)

Not sure why you''d use WaitForSingleObject() unless you''re trying to block the thread that''s waiting on the timer... in which case, why have a timer on a different thread?

...Sorry for lack of code, headed to gym! cheers! :)


可能是,您想知道 ^ ].及其简单使用的API CreateWaitableTimer() [ ^ ], SetWaitableTimer(...) [WaitForSingleObject [ CloseHandle [多媒体计时器 [ ^ ] ,当然.
May be, you would like to know Waitable Timer Objects[^]. And its APIs for simple usage CreateWaitableTimer()[^], SetWaitableTimer(...)[^], WaitForSingleObject[^], CloseHandle[^].


I''ve forgotten to state that there was Multimedia Timers[^], of course.


这篇关于线程而不是计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆