如何处理来自单独线程的窗口消息? [英] How can I handle window messages from a separate thread?

查看:72
本文介绍了如何处理来自单独线程的窗口消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望启动一个单独的线程来处理窗口消息(通过阻塞的GetMessage循环),但此后仍然在初始线程中创建窗口.

I wish to launch a separate thread for handling window messages (via a blocking GetMessage loop), but still create the windows in the initial thread, afterward.

在单独的线程中,一旦启动,我就用PM_NOREMOVE调用PeekMessage,以确保存在消息队列(这是必需的吗?),然后是..

Within the separate thread, as soon as it launches, I am calling PeekMessage with PM_NOREMOVE to ensure a message queue exists (is this necessary?), followed by..

AttachThreadInput(initial thread id,GetCurrentThreadId(),true)

..在最终进入消息循环之前

..before finally entering the message loop

我尚未使用互斥量或cs来确保这种情况及时发生,但是为了简单起见,我仅在初始线程中使用了Sleep语句.

I am not yet using a mutex or cs to ensure this is happening in time, but am merely using a Sleep statement in my initial thread for the sake of simplicity.

无论如何,窗口消息似乎不会被单独的线程拦截.

Regardless, window messages do not appear to be intercepted by the separate thread.

对于我是否正确地执行此操作,我不确定,希望能提供一些指导.两个线程处于同一进程中

I am a little unsure as to whether I am doing this correctly, and would appreciate any possible guidance. Both threads are in the same process

谢谢大家

推荐答案

似乎最好的方法是从主线程中启动窗口创建,而在单独的循环线程中处理它们的消息时,则是使用自定义消息,可以发送到单独的线程-因此允许它创建窗口,但仍允许从初始线程调用该操作:

It seems the best way to instigate window creation from the main thread, while having messages for them handled in a separate, looping thread is to use a custom message, that can be sent to the separate thread - Thus allowing it to create the window, but still allowing that action to be invoked from the initial thread:

1)分配自定义消息,并创建一个结构以保存窗口初始化参数:

1) Allocate a custom message, and create a structure to hold the window initialisation parameters:

message_create_window = WM_USER + 0;
class Message_create_window{
    Message_create_window(...);
};

2)而不是调用CreateWindow(Ex),而是使用类似于以下内容的方法,传入相关的窗口创建参数:

2) Instead of calling CreateWindow(Ex), use something similiar to the following, passing in the relavant window creation parameters:

PostThreadMessage(
    thread.id,
    message_create_window,
    new Message_create_window(...),
    0
);

3)在ui处理线程的消息泵中处理自定义消息,提取创建参数&之后免费:

3) Handle the custom message in the message pump of your ui handling thread, extract the creation parameters, & free afterwards:

MSG msg;
GetMessage(&msg,0,0,0);
...
switch(msg->message){
    ...
    case message_create_window:{
        Message_create_window *data=msg->wParam;
        CreateWindowEx(data->...);
        delete data;
    }break;
    ...

但是,这确实具有以下副作用:

This does, however, have the following side-effects:

  • 该窗口将异步创建.如果需要在创建窗口之前将初始线程阻塞(或者实际上可以断言该窗口的存在),那么必须使用线程同步工具(例如事件)
  • 与窗口进行交互时应特别小心(毕竟它是一个多线程应用程序)

如果此答案中有任何重大漏洞,或者这似乎是一种糟糕的方法,请纠正我. (这仍然是我的问题,并且我正在尝试找到实现这一目标的最佳方法)

If there are any major holes in this answer, or this seems like a terrible approach, please correct me. (This is still my question, & I am trying to find the best way to accomplish this)

这篇关于如何处理来自单独线程的窗口消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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