15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Lost thread messages, or: What does DispatchMessage do when

Noel Burton-Krahn -- noel@harleystreet.com
Wednesday, October 02, 1996

Environment: Win95, VC4.2

Problem: Messages sent with PostThreadMessage() are sometimes not  =
received in my app's main message pump.  Why?

I have a worker thread in my MFC app which communicates with my main =
thread by PostThreadMessage().  The problem is, that while I resize my =
main window, these messages never get recieved by my main message pump.

My latest theory goes like this:  during resize, someone else (such as a =
dialog box) calls GetMessage(msg, NULL, ...) and removes my thread =
message from the queue.  Then, they call DispatchMessage().  The problem =
is, DispatchMessage is only defined for messages with a valid hWnd.  =
What happens to thread messages (where hWnd=3D=3D0)?  I suspect that =
these messages are thrown away before they can arrive at my main message =
pump.

The documentation for DispatchMessage doen't mention the case where =
hWnd=3D=3DNULL.  Anyone know for sure?

Thanks in advance,
--Noel

-----------------------------------------------------
Noel Burton-Krahn
noel@harleystreet.com






Kostya Sebov -- sebov@is.kiev.ua
Wednesday, October 09, 1996

>   Environment: Win95, VC4.2
>
>   Problem: Messages sent with PostThreadMessage() are sometimes not  =
>   received in my app's main message pump.  Why?
>
>   I have a worker thread in my MFC app which communicates with my main =
>   thread by PostThreadMessage().  The problem is, that while I resize my =
>   main window, these messages never get recieved by my main message pump.
>
>   My latest theory goes like this:  during resize, someone else (such as a =
>   dialog box) calls GetMessage(msg, NULL, ...) and removes my thread =
>   message from the queue.  Then, they call DispatchMessage().  The problem =
>   is, DispatchMessage is only defined for messages with a valid hWnd.  =
>   What happens to thread messages (where hWnd=3D=3D0)?  I suspect that =
>   these messages are thrown away before they can arrive at my main message =
>   pump.
>
>   The documentation for DispatchMessage doen't mention the case where =
>   hWnd=3D=3DNULL.  Anyone know for sure?
>
>   Thanks in advance,
>   --Noel
>
>   -----------------------------------------------------
>   Noel Burton-Krahn
>   noel@harleystreet.com
>

I think I had the same problem when trying to handle thread messages when the
modal dialog was being displayed. Probably my case is attributed to the MFC
versions prior to yours but the origin remains the same: to safely exchange
thread messages (that is messages to NULL window) you must ensure that its
YOUR code that runs message loop. "YOUR" means that the loop is not hidden
somewhere in the USER.EXE (MFC mesage pump is OK). This seems to be extremely hard
(almost impossible) if the target thread is a conventional UI one.

That's why I considered this technique as unreliable and recommend to create
a dummy window that would serve as a connection point and that would handle
only your sync message(s) directing them somewhere else. In this case the
message sent will hav a valid HWND and will be properly dispatched even by
hidden system message loops.

HTH

--- 
Kostya Sebov. 
----------------------------------------------------------------------------
Tel: (38 044) 266-6387 | Fax: (38 044) 266-6195 | E-mail: sebov@is.kiev.ua




| Вернуться в корень Архива |