CreateProcess和Parent Threads [英] CreateProcess and Parent Threads
问题描述
我的加密狗心跳有问题。我们一直在使用计时器向我们的加密狗发送心跳以保持许可证的存活。但是,如果我们的一个处理器密集型子进程需要比加密狗许可证超时更长的时间,则需要重新启动应用程序。
有问题的子进程涉及运行第三方控制台应用程序并将其输出重定向到我们的日志文件。此子应用程序使用 :: CreateProcess
启动。由于 WM_TIMER
是低优先级,心跳计时器消息会被分流并且许可证到期。
所以我认为必要的是将心跳计时器放入自己的线程中。我做了这个,但 :: CreateProcess
似乎阻止了父应用程序中的所有线程。
我不喜欢我不想异步运行子进程,那么如何让我的心跳线程同时保持运行?
其他:
我的计时器线程基本上是这样的:
线程和定时器 [ ^ ]
我刚注意到我的控制台重定向类(可选)在 WaitForSingleObject
上执行了创建进程,大概是阻塞父线程的进程。基本上,我需要我的父应用程序继续其后台线程,同时等待生成的进程完成(不使用WaitForSingleObject?)。
I'm having a problem with my dongle heartbeat. We have been using a timer to send a heartbeat to our dongle to keep the license alive. However, if one of our processor-intensive subprocesses takes longer than the dongle license timeout, the application needs to be restarted.
The subprocess in question involves running a third party console app and redirecting its output to our log file. This sub-app is launched using ::CreateProcess
. Since WM_TIMER
is a low priority, the heartbeat timer messages get shunted off and the license expires.
So I thought what was necessary was to put the heart-beat timer into its own thread. I did this, but the ::CreateProcess
appears to block all threads in the parent app.
I don't want to run the sub-process asynchronously, so how do I get my heart-beat thread to keep running at the same time?
Additional:
My timer thread is essentially this:
Threads And Timers[^]
And I just noticed that my console redirection class (optionally) does a WaitForSingleObject
on the created process, which is presumably what blocks the parent threads. Essentially, I need my parent app to continue with its 'background' threads while waiting for the spawned process to finish (without using WaitForSingleObject?).
推荐答案
从更新问题和评论,这似乎解决了这个问题:
使用Windows计时器通常不是最佳选择。对于与系统负载硬件相关和/或敏感的定期操作,工作线程可能是更好的解决方案:
- 创建一个工作线程和一个终止事件。
- 在工作线程中使用
WaitForSingleObject()
,使用terminate事件句柄和你的心跳在一个while循环中超时。- 在终止事件时,离开循环并从线程返回。
- 超时后,执行心跳功能。
- 程序退出时,触发终止事件。
From the updated question and the comments, this seems to solve the problem:
Using the Windows timers is often not the best choice. For periodic actions that are hardware related and/or sensitive to system load, a worker thread may be a better solution:
- Create a worker thread and a terminate event.
- In the worker thread use
WaitForSingleObject()
with the terminate event handle and your heart beat timeout inside a while loop.- Upon the terminate event, leave the loop and return from the thread.
- Upon time out, execute your heart beat function.
- Upon program exit, fire the terminate event.
这篇关于CreateProcess和Parent Threads的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!