Re: Closing modeless dialogs
Torsten Hensel wrote:
David Wilkinson wrote:
Always use PostMessage() (or SendMessage() if you are careful) to
transmit information back to the main thread. This changes the thread
context back to that of the main thread.
I know this, but I don't know why. Currently I'm simply calling a member
function of my dialog called SetText from the worker thread, and
everything works fine. Why is this a problem? Race conditions cannot
occur in my case, since nothing changes the dialog except my worker
thread. The dialog simply waits for messages, but no messages are sent
to the dialog. Can you explain what problems can occur in this case?
Torsten:
Well, in MFC there is the problem that the window map (mapping from HWND's to
CWnd's) is maintained on a per-thread basis. So you need to be sure that you do
not do anything that depends on this mapping, which in principle requires
knowing the inner workings of MFC.
In Win32 in general, most API's that manipulate windows use SendMessage()
internally, and so there is some possibility of locking the main thread. But I
do not see how this can happen in your case. I have to admit that in these
situations where the main thread is not doing anything except keeping the GUI
alive I do use inter-thread SendMessage() explicitly, and this has always worked
for me.
Maybe there are other pure Win32 reasons, I'm not sure.
--
David Wilkinson
Visual C++ MVP