每个循环可能运行也可能不运行 CDialog 的无限线程

发布于 2024-09-19 11:37:44 字数 129 浏览 1 评论 0原文

我正在开发一个 MFC 项目,其中需要一个单独的循环,该循环将连续运行或每隔几秒运行一次,并且每次可能需要也可能不需要运行对话框来获取用户的一些输入。我正在考虑使用 AfxBeginThread,但从我读到的内容来看,它并不能真正用于连续循环。

I am working on a MFC project where I need a separate loop that will run continually or once every few seconds, and each time it may or may not need to run a Dialog to get some input from the user. I was thinking of using AfxBeginThread, but from what I have read about it, it doesn't really work with a continuous loop.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放飞的风筝 2024-09-26 11:37:44

不要这样做。你不能只是在工作线程中撕掉对话框。它们只能在主线程中启动,因为它们需要消息泵才能运行。

如果您想要的只是每隔几秒触发一次的某种信号,那么您想要的是 计时器。将计时器设置为您想要的计时器周期,当您的主线程处理所需的 WM_TIMER 消息时,您可以弹出一个对话框并执行您的操作。

如果您希望工作线程执行某些工作(可能包括也可能不包括询问用户信息),那么您需要考虑让您的线程使用 PostMessage() 将消息发送到主线程,通过询问用户在主线程中处理该消息数据,然后将信号连同输入数据发送回工作线程。完成最后一点的一种方法是调用 QueueUserAPC() 来自主线程,带有工作线程的句柄和指向对新输入的数据执行某些操作的函数的指针。

Don't do it. You can't just rip dialogs off in worker threads. They can only be started in the main thread, because they need the message pump in order to function.

If all you want is a signal of some kind that fires every few seconds, then what you want is a timer. Set the timer for the timer period you want, and when your main thread processed the desired WM_TIMER message, you can pop up a dialog and do your thing.

If you want your worker thread to do some work, which may or may not include asking the user for information, then you'll want to look in to having your thread use PostMessage() to send a message to the main thread, process that message int he main thread by asking the user for data, and then sending a signal back to the worker thread with the input data. One way to accomplish the last bit is by calling QueueUserAPC() from the main thread with the worker thread's handle and a pointer to a function that does somethething with the newly-entered data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文