473,811 Members | 3,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*)Af xGetApp()->m_pMainWnd->GetDescendantW indow(AFX_IDW_S TATUS_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 2003
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*)Af xGetApp()->m_pMainWnd->GetDescendantW indow(AFX_IDW_S TATUS_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******@chefm ail.de> schrieb im Newsbeitrag
news:11******** *************@g 43g2000cwa.goog legroups.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*)Af xGetApp()->m_pMainWnd->GetDescendantW indow(AFX_IDW_S TATUS_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
22181
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 assigned a name. As in .net there is no way we can get the managed threads, I am thinking of making a win32 call and check their status.
3
4988
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
34252
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 1.07/Linux, Firefox 1.5/Linux, Firefox 1.5/Windows and Firefox 1.5/Mac, Safari/Mac. It works perfectly on a lot of configurations but, on some PC with Firefox 1.5/Windows (not all), the Javascript code with XmlHttpRequest
0
286
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 time this works ok and I get a sane response from the server. However, every night within the same broad time window of 7 hours, I get a relatively high incidence of WebException's being thrown. The status of this is "UnknownError". When I try to...
1
1651
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 the time this works ok and I get a sane response from the server. However, every night within the same broad time window of 7 hours, I get a relatively high incidence of WebException's being thrown. The status of this is "UnknownError". When I try...
1
3043
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 I want, which will in turn display in the status bar. it works great. my problem is that when I do database calls, or use SMO to get all the available SQL servers, or other processor intensive stuff, the status bar locks up till its done.
1
5804
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 should delay my text updating by. Correct? When you mentioned a delay - is there an other way to do it than threads? I've found this solution.
0
1269
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. In the VS 2005 output window I was expecting to see thread names within each thread exit line, but I am not seeing any. For example, I am seeing lines similar to this one
9
4127
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 in another class from the main class. So it couldn't access the Status Line or textbox. So what we did was set them up as properties: string IStatusDisplay.Status
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10386
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10398
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.