工作线程没有消息循环(MFC、Windows)。我们可以让它接收消息吗?
Mfc 提供工作线程和 UI 线程。 UI 线程启用了消息接收功能(发送、发布)。是否可以让工作线程也接收消息。
Mfc provides both worker and UI thread. UI thread is enabled with message receiving capabilities (send, post). Could it be possible to let worker thread too receive messages.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
重复调用
CWinThread::PumpMessage()
,直到返回WM_QUIT
消息。Call
CWinThread::PumpMessage()
repeatedly until it returns aWM_QUIT
message.看来您需要一个可以处理来自另一个线程的多条消息的线程。另一个线程会将消息添加到该线程的消息队列中。那么,在这种情况下,您可以使用
PeekMessage
启动一个循环,最终创建一个隐藏窗口,然后使用GetMessage
获取消息。其他线程将使用PostThreadMessage
以及线程 ID(具有 Peek/GetMessage 的线程 ID)和消息代码,LPARAM
、WPARAM。
它就像(语法上不正确):
线程将调用
PostThreadMessage
- 有关详细信息,请参阅 MSDN。当您需要发送的数据多于 LPARAM/WPARAM 可以容纳的数据时,您最终需要在堆上分配它们,然后在自定义消息循环中处理消息后删除它们。这会很麻烦并且有问题。
但是...我建议您在
std::queue/deque
或其他 DS 之上拥有自己的类,您可以在其中添加AddMessage
/PushMessage
和PopMessage
(或任何您喜欢的名称)。您需要使用SetEvent
、WaitForSingleObject
来循环触发新消息(请参阅此处实现。您可以将其设为通用的一种数据类型,或将其设为模板类 - 它将支持任何数据类型(您的底层 DS (queue
)将使用相同的数据类型)。但是,您可能需要处理使用 Windows 事件的 问题。内核模式转换(因为事件是命名/内核对象),并且您可能喜欢使用作为用户对象的条件变量。或者您可以直接使用 VC10 中可用的并发运行时库中的
unbounded_buffer
类。 本文(跳转至unbounded_buffer
)。It seems you need a thread, that can handle multiple messages from another threads. Another threads would add-a-message to the message-queue of this thread. Well, in that case you may use
PeekMessage
to startup a loop, which would eventually create a hidden window, and then useGetMessage
to get the messages. The other threads would usePostThreadMessage
with the thread ID (the one having Peek/GetMessage), and the message-code,LPARAM
,WPARAM.
It would be like (not syntactically correct):
The threads would call
PostThreadMessage
- See MSDN for more info.When you need to send more data than LPARAM/WPARAM can hold, you eventually need to allocate them on heap, and then delete AFTER processing the message in your custom message-loop. This would be cumbersome and buggy.
But... I would suggest you to have your own class, on top of
std::queue/deque
or other DS, where you can addAddMessage
/PushMessage
, andPopMessage
(or whatever names you like). You need to useSetEvent
,WaitForSingleObject
to trigger the new message in loop (See one of the implementation here. You may make it generic for one data-type, or make it template class - that would support any data-type (your underlying DS (queue
) would utilize the same data-type). You also need not to worry about heaps and deletions. This is less error prone. You may however, have to handle MT issues.Using Windows events involves kernel mode transition (since events are named/kernel objects), and you may like to use Conditional Variables which are user objects.Or you may straightaway use
unbounded_buffer
class from Concurrency Runtime Library available in VC10. See this article (jump tounbounded_buffer
).是的,您可以在工作线程上创建消息队列。您将需要在该线程上运行消息泵。
Yes you can create a message queue on a worker thread. You will need to run a message pump on that thread.