Re: Need help solving a threading issue

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



See below...
On Mon, 30 Jul 2007 06:50:15 -0700, "David Ching" <dc@xxxxxxxxxxxxxxxxxxxxxx> wrote:

"Joseph M. Newcomer" <newcomer@xxxxxxxxxxxx> wrote in message
news:ccpra3d5g8ci87f9ubm5q2ure7bh8e7jhe@xxxxxxxxxx
If the HWND is not a top-level owned window, you end up in trouble,
because the
complexities of interthread interaction are going to be a source of
potential problems.
For example, the way CAsyncSocket gets its callbacks is to create a
top-level invisible
window (hence it has no parent to have to send notifications to), and when
a callback
(represented by a SendMessage to the window) is received, the SOCKET value
passed in is
looked up in the thread's handle map, and the CAsyncSocket class object is
retrieved. The
virtual methods (such as OnReceive, OnSend, etc.) are called, which causes
the virtual
method of your derived class to be called.

But having a thread create a child HWND, or a CDialog, or something like
that, opens up
opportunities for deadlock and similar nasty behavior, which are best
avoided.

I would say these issues are the same regardless of whether they are
performed on the primary UI thread or a secondary UI thread. The fact that
CAsyncSocket creates it's own hidden window confirms that it is fine to
create HWND's on a secondary thread.
****
That's what I was confirming. There's nothing wrong with creating HWNDs in a secondary
thread, but the issues really *are* different if you create child windows, dialogs, etc.
in a secondary thread. There are two issues here, one dealing with thread message queues,
and another dealing with interthread SendMessage, both of which can be avoided by simply
not creating user-visible HWNDs in a secondary thread. There aren't any significant
issues if a child window is created in the main GUI thread.
****

I agree a child HWND whose parent was created in another thread is an issue,
but again it is an issue regardless of whether the child is part of a
secondary UI thread or not. A CDialog can be a top-level, owned window, so
I don't see an issue with creating one on the secondary UI thread either
(but I agree a CDialog is not the right choice for a hidden window).
****
But the whole point here is that we are discussing whether or not these *are* issues if
they are created in a secondary thread. If the window is created in the main GUI thread,
there isn't any set of issues to worry about, because the parent is in the same thread and
there can be no cross-thread messaging as the child communicates to the parent or a dialog
generates events to its parent.

It is actually hard to create a dialog as a top-level owned window in MFC, because it
would have a parent of NULL, but in MFC, if you specify the parent as NULL, then it
defaults to using the main application window as its parent, and there isn't really a way
to specify that the parent HWND is NULL, because the only expression the programmer has is
to specify the parent CWnd* as NULL, which has a different meaning in how it is
interpreted.
joe

****

-- David



Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Casting CWnd-derived Pointers ?
    ... you should not be accessing member functions of a parent window from a child ...
    (microsoft.public.vc.mfc)
  • Re: Win32 - forked process doesnt create a Window
    ... > The only thing missing is the graphical window for NOTEPAD so I can type ... returns zero to the child. ... you are exec'ing from the parent ...
    (comp.lang.perl)
  • Re: when and how do the the system inform the CFormView to redraw ?
    ... Thanks, Joe. ... Is it send WM_PAINT to parent earlier than childrens? ... > rectangle to keep drawing from spilling out onto the parent window; ... >>only the child ...
    (microsoft.public.vc.mfc)
  • Re: System.ArgumentException on close when dialog not on top
    ... I'm not trying to close the parent of a child. ... close the parent of an active window without closing that window, ... the parent that no longer contains sensible data, causing the exception. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Composite. Again!
    ... >> In order to hide its child components the parent needs to become a ... Which means that every operation required to invoke on child ... >> needs to be passed thought parent. ... > Then how does the Window object know which interfacea view implements? ...
    (comp.object)