PostMessage() and re-entrancy

PostMessage() and re-entrancy

Post by Bogda » Wed, 01 Mar 2006 07:04:19


Hi,

In a CView derived class I have a method that posts messages to the view.
The method is intended to be used by worker threads that require the view to
perform certain actions (e.g. update screen, open/close ActiveX control,
etc.). The view has an assoc. message handler that acts on worker threads
requests. The pseudo-code is as follows:

// Called by a worker thread (i.e. non-UI thread)
CMyView::PerformAction()
{
/* ... */
PostMessage(WM_USER, wParam, lParam);
}

// Message handler (main UI thread)
LRESULT CMyView::OnUser()
{
/* .. */
}

The problem I've run into is that OnUser() is sometimes called recursively.
One example would be when PeformAction() is called while OnUser() is still
acting (e.g. destroying an ActiveX control) on a previously posted message.
It looks like destroying a control in OnUser() can trigger the view's
message queue to be emptied which in turn ends up calling OnUser() handler.

Could someone please explain why this is happening or point me to a specific
doc that describes it?

Thanks,
Bogdan
 
 
 

PostMessage() and re-entrancy

Post by Scott McPh » Wed, 01 Mar 2006 07:48:31


The docs for CWnd::DestroyWindow say, among other things, that it
"flushes the application queue." That is probably what you are seeing.
If a message handler function pumps messages, or causes more messages
to be sent, it is not unusual for the wndProc (and therefore the message
pump) to reenter.

--
Scott McPhillips [VC++ MVP]

 
 
 

PostMessage() and re-entrancy

Post by Bogda » Wed, 01 Mar 2006 10:53:39

Scott,

Thanks for your answer. Description of DestroyWindow() is exactly what I
was looking for. I just did not realize (or did not remember) that
destroying active x created in the view on the main UI thread will flush
that thread's queue when the active x is destroyed.

Bogdan
 
 
 

PostMessage() and re-entrancy

Post by Victo » Thu, 02 Mar 2006 05:40:40

See below...



to

You should use IDs in the range (WM_APP through 0xBFFF) for your private
messages, some of IDs in the range (WM_USER through 0x7FFF) are already used
by some predefined window classes such as BUTTON, EDIT, LISTBOX, COMBOBOX,
...


Your message handler signature is incorrect. It should be:
LRESULT CMyView::OnUser(WPARAM, LPARAM)
{
/* .. */
}

Regards,
Victor