6
votes

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

2
This is almost always a terrible idea. For starters, you probably want to handle messages that aren't actually posted to the UI thread's input queue... Failing that, you'll at least want to send messages to windows owned by the UI thread, and that won't work either. Really, you should be processing messages on the UI thread, and using the separate thread for time-consuming operations, without trying to share the input queue between them. See: stackoverflow.com/questions/783073/…Shog9
For all intents & purposes I wish to use the separate thread as the ui thread. I just wish to have the ability to instigate the window creation from the primaryProPuke
@ProPuke: and you can't do that. Windows must be owned and manipulated by one and only one thread: the entire system is designed with this assumption.Shog9
Can I pass ownership to another thread after creation?ProPuke
@ProPuke: no. And what would that accomplish? You'd have to synchronize creation and manipulation anyway, so why not just create them on the thread that will be manipulating them! Keep in mind also: you shouldn't be mixing thread ownership within a given window hierarchy (top-level windows on one thread, children on another), or you'll end up with the same sort of problems.Shog9

2 Answers

8
votes

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.

2
votes

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)