Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2006

Tip: Looking for answers? Try searching our database.

Dialog created within a thread

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Victor - 31 May 2006 21:42 GMT
Hi everybody,

could anybody kindly give me a clue in dealing with a following problem

I create a modeless dialog by means of CreateDialogIndirect(). The call
executes within a thread and the returned HWND is assigned to a global
variable. The dialog appears and can be properly operated, but it
exists only this long as the thread where the call happens remains
alive. On closing the thread the dialog disappears the same moment.
I would like to keep it independent on the thread, though its creation
(and displaying) should be initiated by the logic flow of the thread.

My development tool is VS 2005 C++ under WinXP.

How can I achieve this goal?

Thanks in advance

Victor
William DePalo [MVP VC++] - 31 May 2006 22:10 GMT
> I create a modeless dialog by means of CreateDialogIndirect(). The call
> executes within a thread and the returned HWND is assigned to a global
[quoted text clipped - 3 lines]
> I would like to keep it independent on the thread, though its creation
> (and displaying) should be initiated by the logic flow of the thread.

That's not possible. The lifetime of windows and the lifetime of the threads
that own them are inextricably bound.

Signature

Regards,
Will

"I'm not ready to make nice, I'm not ready to back down ..."

Victor - 01 Jun 2006 17:05 GMT
> That's not possible. The lifetime of windows and the lifetime of the threads
> that own them are inextricably bound.

Thank you, William.

Could you however comment it a little bit more?

I believe it's not this straight and forward in my case.
The thread call for CreateDialogIndirect() looks like this :

dlgRoute =
CreateDialogIndirect(instance,(LPCDLGTEMPLATE)hgbl,hwnd,(DLGPROC)routeProc);

Here are

dlgRoute      HWND            global varialbe
instance       HINSTANCE   global variable, handle from the
InitInstance() call during startup of the application
hgbl             HGLOBAL      local variable of the thread
hwnd           HWND            global variable, returned by the
CreateWindow() call from within the mentioned InitInstance() thus being
a handle to the top window.

Does it mean that the owner of the dialog must be the app top window?
According to the MSDN, the parameter 'hwnd' specifies 'Handle to the
window that owns the dialog box'. Is it not my case? And this owner
happily continues to exist after the thread ends up...

Victor
Carl Daniel [VC++ MVP] - 01 Jun 2006 21:36 GMT
>> That's not possible. The lifetime of windows and the lifetime of the
>> threads
[quoted text clipped - 24 lines]
> window that owns the dialog box'. Is it not my case? And this owner
> happily continues to exist after the thread ends up...

There is a requirement throughout Windows - every version - that a window
can only be accessed by the thread that created it.  Any attempt to access
an HWND from another thread results in a SendMessage to the owning thread,
which must handle the message before the requesting thread can un-block.
This is a common source of deadlocks when windows are owned by multiple
threads.

If your worker thread needs to terminate before the lifetime of the dialog
is completed, then you need to create the dialog in another thread - one
that outlives the dialog.

It's usually best to have all of the windows created by the main thread, but
it's not required.

-cd
William DePalo [MVP VC++] - 01 Jun 2006 21:49 GMT
> Thank you, William.

You are welcome.

> Could you however comment it a little bit more?

I see that Carl has already explained the issue. To add a little more
information, you have to realize that the message queue for a window is
attached to the thread that owns it. When the thread dies, the queue is
destroyed because there is _no_ longer any way in which a _posted_ message
could be removed from it. In fact, I found (in an application of mine which
had a bug that caused a premature thread death) that the window manager
would summarily remove an "orphaned" window from the screen without the
usual flurry of messages, e.g. WM_CLOSE, WM_DESTROY, WM_NCDESTROY etc.

Signature

Regards,
Will

"I'm not ready to make nice, I'm not ready to back down ..."


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.