HOWTO: Create Windows in a Multithreaded Application

ID: Q90975

The information in this article applies to:

SUMMARY

In a multithreaded application, any thread can call the CreateWindow() API to create a window. There are no restrictions on which thread(s) can create windows.

It is important to note that the message loop and window procedure for the window must be in the thread that created the window. If a different thread creates the window, the window won't get messages from DispatchMessage(), but will get messages from other sources. Therefore, the window will appear but won't show activation or repaint, cannot be moved, won't receive mouse messages, and so on.

MORE INFORMATION

Normally, windows created in different threads process input independently of each other. The windows have their own input states and the threads are not synchronized with eachother in regards to input processing.

In order to have threads to share input state, have one thread call AttachThreadInput() to have its input processing attached to another thread. What this means is that these two threads will use a Windows 3.1 style system queue. The threads will still have separate input, but they will take turns reading out of the same queue.

Creating a window can force an implicit AttachThreadInput(), when a parent window is created in one thread and the child window is being created in another thread. When windows are created (or set) in separate threads with a parent-child relationship, the input queues are attached.

Additional query words:

Keywords          : kbNTOS kbGrpUser kbWinOS95 kbWinOS98 kbWndw 
Issue type        : kbhowto

Last Reviewed: December 26, 1998