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++ / October 2005

Tip: Looking for answers? Try searching our database.

Status bar output from within a thread

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Victor - 16 Oct 2005 17:36 GMT
Hi everybody!

VS.NET 2003 C++ MFC-SDI-Standard project.

I would like to show some current info in the status bar of the
SDI-Window.
As long as I use the following code

CStatusBar* pStatus =
(CStatusBar*)AfxGetApp()->m_pMainWnd->GetDescendantWindow(AFX_IDW_STATUS_BAR);
pStatus->SetPaneText(0,"Some info...");
pStatus->UpdateWindow();

in a message handler - everything runs perfectly.

Now I have to make the same output from within a thread running in the
same class. This code compiles OK, but when running the program, I get
some troubles with an unhandled exception during executing the function
SetPaneText(). I tried to find the CStatusBar* pointer before starting
the thread and to use it within the thread: the nature of the problems
with the function SetPaneText() changed slightly, but it does not work
nevertheless...

What goes wrong?

Many thanks in advance

Victor
David Wilkinson - 16 Oct 2005 18:08 GMT
> Hi everybody!
>
[quoted text clipped - 24 lines]
>
> Victor

Victor:

Use PostMessage or SendMessage from your secondary thread to your main
thread, and have the main thread update the status bar. It is almost
always best to have all the GUI in the main thread.

HTH,

David Wilkinson
Guido Stercken-Sorrenti [MVP VC++] - 16 Oct 2005 19:15 GMT
Generally, MFC objects (and especially those derived from CWnd) are not
thread-safe and hence can't be used across threads. If you need to
communicate with your UI from a thread, always do this with user-defined
messages. If you need to reference a particular window, use its HWND for
that, never a CWnd pointer.

Signature

Guido Stercken-Sorrenti
MVP - Visual Developer / Visual C++

> Hi everybody!
>
[quoted text clipped - 24 lines]
>
> Victor
Victor - 16 Oct 2005 22:23 GMT
Thank you guys!

A user message works fine !!!

Was about this idea myself... Wondered if there are any other
chances...

Victor
Carl Daniel [VC++ MVP] - 16 Oct 2005 23:37 GMT
> Thank you guys!
>
> A user message works fine !!!
>
> Was about this idea myself... Wondered if there are any other
> chances...

No, it's a fundamental property of Windows - all processing for a given
window occurs in the context of the thread that created the window.  When
you try to update the window from another thread, the system internally
sends a message to the owning window which processes the message.  This
invisible inter-thread communication can lead to all sorts of problems, most
usually deadlocks.  As another respondent suggested, the MFC objects
themselves are not designed to be access by multiple thread because of this
natural thread affinity within windows, so accessing an MFC UI element from
another thread can lead to all sorts of errors, including access violations
and crashes of various sorts.

Using any kind of a message eliminates any multi-threading concerns for MFC.

Using post message breaks the deadlock possibility (which still exists with
SendMessage).

-cd
Victor - 17 Oct 2005 19:49 GMT
Thanks once again !

Victor

Rate this thread:







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.