473,327 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Status bar output from within a thread

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

Nov 17 '05 #1
5 1980
Victor wrote:
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
Nov 17 '05 #2
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.

--
Guido Stercken-Sorrenti
MVP - Visual Developer / Visual C++
"Victor" <bi******@chefmail.de> schrieb im Newsbeitrag
news:11*********************@g43g2000cwa.googlegro ups.com...
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

Nov 17 '05 #3
Thank you guys!

A user message works fine !!!

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

Victor

Nov 17 '05 #4
Victor wrote:
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


Nov 17 '05 #5
Thanks once again !

Victor

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: bygandhi | last post by:
Hi - I am writing a service which will check a process and its threads for their state ( alive or dead ). The process has 5 .net managed threads created using thread.start and each have been...
3
by: Norton | last post by:
Hi all, May i know how to get the network status through WMI, i have searched the WMI properties but cannot find the related property to use. thx in advance Norton
42
by: Greg | last post by:
Hi, I've designed a bookmark in Ajax / PHP that I will put soon on sourceforge.net. But I've got an very tricky bug. I try it on some computers with Internet Explorer/Windows, Firefox...
0
by: Greg | last post by:
My app makes a call to a remote server (about which I know relatively little) using Webclient.UploadData. This is done once every 51/2 mins and is called by code within a worker thread. 95% of the...
1
by: Greg | last post by:
My app makes a call to a remote server (about which I know relatively little) using Webclient.DownloadData. This is done once every 51/2 mins and is called by code within a worker thread. 95% of...
1
by: =?Utf-8?B?Um9nZWxpbw==?= | last post by:
hey, I have a status bar, and a timer control, and a statc variable. every 0.5 seconds, the timer sets the status bar text to the variable. then I can just change the variable's value whenever...
1
by: K Viltersten | last post by:
> bar.PerformStep (); I noticed that there's a property for the bar stating what time it should take for the animation to move the fill to the requested spot. I'm guessing that's the time i...
0
by: franzbrown | last post by:
Most of the threads I am using in my C# application have names (Thread.Name = ). When I am debugging my application within VS 2005, I have a UI to stop my application and the threads it started....
9
by: tshad | last post by:
I have a Windows App that is doing some work and then writing a "Now Processing..." line to the status line of the window as well as the Textbox on the form. But the problem is that the work is...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.