工作线程没有消息循环(MFC、Windows)。我们可以让它接收消息吗?

发布于 2024-11-19 11:58:59 字数 63 浏览 2 评论 0原文

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 技术交流群。

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

发布评论

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

评论(3

ˉ厌 2024-11-26 11:58:59

重复调用CWinThread::PumpMessage(),直到返回WM_QUIT消息。

Call CWinThread::PumpMessage() repeatedly until it returns a WM_QUIT message.

薄荷港 2024-11-26 11:58:59

看来您需要一个可以处理来自另一个线程的多条消息的线程。另一个线程会将消息添加到该线程的消息队列中。那么,在这种情况下,您可以使用 PeekMessage 启动一个循环,最终创建一个隐藏窗口,然后使用 GetMessage 获取消息。其他线程将使用 PostThreadMessage 以及线程 ID(具有 Peek/GetMessage 的线程 ID)和消息代码,LPARAMWPARAM。

它就像(语法上不正确):

TheProcessor()
{
    MSG msg;
    PeekMessage(&msg,...);

    while(GetMessage(&msg...)
    {    /* switch case here */ }
}

线程将调用 PostThreadMessage - 有关详细信息,请参阅 MSDN。
当您需要发送的数据多于 LPARAM/WPARAM 可以容纳的数据时,您最终需要在堆上分配它们,然后在自定义消息循环中处理消息后删除它们。这会很麻烦并且有问题。

但是...我建议您在 std::queue/deque 或其他 DS 之上拥有自己的类,您可以在其中添加 AddMessage/PushMessagePopMessage (或任何您喜欢的名称)。您需要使用 SetEventWaitForSingleObject 来循环触发新消息(请参阅此处实现。您可以将其设为通用的一种数据类型,或将其设为模板类 - 它将支持任何数据类型(您的底层 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 use GetMessage to get the messages. The other threads would use PostThreadMessage with the thread ID (the one having Peek/GetMessage), and the message-code, LPARAM, WPARAM.

It would be like (not syntactically correct):

TheProcessor()
{
    MSG msg;
    PeekMessage(&msg,...);

    while(GetMessage(&msg...)
    {    /* switch case here */ }
}

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 add AddMessage/PushMessage, and PopMessage (or whatever names you like). You need to use SetEvent, 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 to unbounded_buffer).

花之痕靓丽 2024-11-26 11:58:59

是的,您可以在工作线程上创建消息队列。您将需要在该线程上运行消息泵。

Yes you can create a message queue on a worker thread. You will need to run a message pump on that thread.

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