0

Is there limitations – in which threads) must be (1) created window (RegisterClassEx, CreateWindowExW) and (2) performed messages processing loop ?:

    MSG msg = { 0 };
    while (GetMessage(&msg, 0, 0, 0)) {
        TranslateMessage(&msg); 
        DispatchMessage(&msg);  
    }

In my implementation (1) is called from the main thread, (2) is called from the new thread, specially created to get window messages. This is console application, so I need to manually create the new window to get window messages of certain type (messages from DirectShow; not for GUI) (Win10, VS 2022).

LUN2
  • 75
  • 5
  • 1
    Does this answer your question? [Creating window in another thread(not main thread)](https://stackoverflow.com/questions/9654532/creating-window-in-another-threadnot-main-thread) – Adrian Mole Jun 12 '22 at 11:48
  • Adrian. thank you for your reply. If I understood the mentioned answer correctly, I can create windows and message processing loops in another thread. So, the steps (1) and (2) must be located in one thread (not necessarily in the main thread). Is it right ? – LUN2 Jun 12 '22 at 12:05
  • 1
    yes, message loop must be in the same thread, which create window(s). and this can be any thread. not exist "main" thread – RbMm Jun 12 '22 at 12:14
  • @RbMm, thank your for the reply. I have used the term "main thread" to signify a thread where may main() procedure started. OK, I will create window and place processing loop in the separate thread. DirectShow requires to point a window, where it will post your messages (hWnd): ``` lang-cpp HRESULT h_noti = g_pEvent->SetNotifyWindow((OAHWND)hWnd, WM_GRAPHNOTIFY, 0); ``` So, can I pass the window handle (hWnd), created in the separate thread, to SetNotifyWindow(..) that called from the "main" thread ? Will the messages come correctly ? – LUN2 Jun 12 '22 at 12:45
  • From [Thread affinity of user interface objects, part 1: Window handles](https://devblogs.microsoft.com/oldnewthing/20051010-09/?p=33843): *"The thread that creates a window is the one with which the window has an inseparable relationship. [...] **Messages are dispatched to a window procedure only on the thread that owns it**"*. – IInspectable Jun 13 '22 at 09:12
  • @LUN2 yes, any thread can post/send messages to an HWND in another thread. Only the thread that created the HWND can receive those messages. – Remy Lebeau Jun 13 '22 at 19:03

0 Answers0