HOWTO: Create Windows in a Multithreaded Application

ID: Q90975


The information in this article applies to:
  • Microsoft Win32 Software Development Kit (SDK)
  • Microsoft Windows 2000


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.

On Windows NT/2000 you cannot use AttachThreadInput to attach a thread to a thread in another desktop.

Additional query words:

Keywords : kbNTOS kbWinOS2000 kbSDKWin32 kbGrpUser kbWinOS95 kbWinOS98 kbWndw
Version : WINDOWS:
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: February 3, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.