Re: Need help solving a threading issue
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Mon, 30 Jul 2007 14:53:54 -0400
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
****
Joseph M. Newcomer [MVP]
-- David
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: Need help solving a threading issue
- From: David Ching
- Re: Need help solving a threading issue
- References:
- Need help solving a threading issue
- From: shadowulf
- Re: Need help solving a threading issue
- From: Scott McPhillips [MVP]
- Re: Need help solving a threading issue
- From: shadowulf
- Re: Need help solving a threading issue
- From: David Wilkinson
- Re: Need help solving a threading issue
- From: David Ching
- Re: Need help solving a threading issue
- From: Joseph M . Newcomer
- Re: Need help solving a threading issue
- From: David Ching
- Need help solving a threading issue
- Prev by Date: Re: Need help solving a threading issue
- Next by Date: Re: Another SetTimer question
- Previous by thread: Re: Need help solving a threading issue
- Next by thread: Re: Need help solving a threading issue
- Index(es):
Relevant Pages
|