工作线程没有消息循环(MFC、Windows).我们可以让它接收消息吗? [英] Worker thread doesn't have message loop (MFC, windows). Can we make it to receive messages?

查看:29
本文介绍了工作线程没有消息循环(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.

推荐答案

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

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 */ }
}

线程将调用 PostThreadMessage - 有关详细信息,请参阅 MSDN.当您需要发送超过 LPARAM/WPARAM 可以容纳的数据时,您最终需要在堆上分配它们,然后在自定义消息循环中处理消息后删除.这将是麻烦和错误的.

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.

但是...我建议您在 std::queue/deque 或其他 DS 之上拥有自己的类,您可以在其中添加 AddMessage/PushMessagePopMessage(或任何你喜欢的名字).您需要使用 SetEventWaitForSingleObject 来触发循环中的新消息(参见 此处实现.您可以将其设为一种数据类型的泛型,或设为模板类 - 支持任何数据类型(您的底层 DS(queue) 将使用相同的数据类型.您也不必担心堆和删除.这不太容易出错.但是,您可能必须处理 MT 问题.

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.

使用 Windows 事件涉及内核模式转换(因为事件是命名/内核对象),您可能喜欢使用作为用户对象的条件变量.或者您可以直接使用 Concurrency Runtime 中的 unbounded_buffer 类VC10 中可用的库.请参阅这篇文章(跳转至unbounded_buffer).

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).

这篇关于工作线程没有消息循环(MFC、Windows).我们可以让它接收消息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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