Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I handle window messages from a separate thread?

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.

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

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

Thank you all

like image 526
ProPuke Avatar asked Dec 26 '09 19:12

ProPuke


2 Answers

That's not what AttachThreadInput does. Even after you attach your input queue to another thread, Windows still have thread affinity. Messages in the queue for a given window can only be removed from the queue by that window's thread.

What AttachTheadInput does is to make two threads share an input queue. This allows them to query information about the input state and know that the other thread will get the same answer for the same query. For instance, one thread could call GetAsyncKeyState and know that the answer reflected the key state for the other thread.

It allows two or more threads to have the same relationship to the input queue and each other as processes had in Windows 3x. This is the reason that this API exists; so that complex multiprocess applications could be ported from Win 3x to Win95/WinNT.

like image 105
John Knoeller Avatar answered Sep 20 '22 02:09

John Knoeller


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

  • The window will be created asynchronously. If it is required that the initial thread block until the window is created (or, indeed, that the window's existence can ever be asserted) then a thread synchronisation tool must be used (such as an event)
  • Care should be taken when interacting with the window (it is a multithreaded application, after all)

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)

like image 37
ProPuke Avatar answered Sep 20 '22 02:09

ProPuke