Status bar output from within a thread

  • Thread starter Thread starter Victor
  • Start date Start date
V

Victor

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
 
Victor said:
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
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
 
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.
 
Thank you guys!

A user message works fine !!!

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

Victor
 
Victor said:
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
 
Back
Top