473,406 Members | 2,404 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,406 software developers and data experts.

change the text of a label while a thread is running

kspiros
16
i can not find a way in order to change a label while this tread is running
Expand|Select|Wrap|Line Numbers
  1. public void work()
  2.         {
  3.             ///////
  4.  
  5.             int[] d = new int[5];
  6.             Thread.Sleep(draw_delay);
  7.                 for (int i = 0; i < 5; i++)
  8.                 {
  9.                     d[i] = 0;
  10.                     double a;
  11.                     a = Math.Sqrt(Math.Pow(pointcheck[0] - X[i], 2) + Math.Pow(pointcheck[1] - Y[i], 2));
  12.                     d[i] = Convert.ToInt32(a);
  13.                 }
  14.                 myTempMap = ptuxiaki.Properties.Resources.map;
  15.                 if (Math.Abs(d[0]) < R[0])
  16.                 {
  17.                    UpdateWiMax_enable();// This method will draw an ellipse for the WiMax signal
  18.  
  19.                 }
  20.                 else {
  21.                     UpdateWiMax();
  22.                 }
  23.                 if (Math.Abs(d[1]) < R[1])
  24.                 {
  25.                     UpdateUMTS_enable();// you get the drift                        
  26.                 }
  27.                 else
  28.                 {
  29.                     UpdateUMTS();
  30.                 }
  31.                 if (Math.Abs(d[2]) < R[2])
  32.                 {
  33.                     UpdateCPC_enable();
  34.                 }
  35.                 else
  36.                 {
  37.                     UpdateCPC();
  38.                 }
  39.                 if (Math.Abs(d[3]) < R[3])
  40.                 {
  41.                     UpdateGPRS_enable();
  42.                 }
  43.                 else
  44.                 {
  45.                     UpdateGPRS();
  46.                 }
  47.                 if (Math.Abs(d[4]) < R[4])
  48.                 {
  49.                     UpdateWiFi_enable();
  50.                 }
  51.                 else
  52.                 {
  53.                     UpdateWiFi();
  54.                 }
  55.                     pictureBox_Map.Image = myTempMap;
  56.                     myTempMap = null;
  57.  
  58.         }
if i do this
Expand|Select|Wrap|Line Numbers
  1. if (Math.Abs(d[0]) < R[0])
  2.                 {
  3.                    UpdateWiMax_enable();
  4. label.text="something";
  5.  
  6.                 }
I get the error that Cross-thread operation not valid: Control 'label' accessed from a thread other than the thread it was created on. who can i solve this?
Nov 11 '08 #1
3 10386
mldisibio
190 Expert 100+
WinForms UI Thread Invokes
MSDN BackgroundWorker
Authoring MultiThreaded Components
You cannot update or modify a control property from any other thread other than the thread the control was created on.

To make sure you are on the correct thread, you use the Invoke method of the control to perform the update.

The preferred threading model for Windows Forms threading is provided by the BackgroundWorker component.
Nov 11 '08 #2
balabaster
797 Expert 512MB
Okay, if you're familiar with delegates, this is a breeze... if not, this is going to take some getting your head around.

If you want to post data to the main thread from another, then you have to post through a delegate method - think of a delegate method as a placeholder for the memory address of a method. We put the address of the method we wish to run into the delegate which then sits and waits to be called. At whatever point the delegate is called - it fires whatever method sits at the address stored in its buffer.

It's been a while since I had to do this, but the following code is close enough to point you in the right direction...

VB.NET
Expand|Select|Wrap|Line Numbers
  1. Public Delegate Sub SetLabelTextDelegate(ByVal LabelObject As Label, ByVal Value As String)
  2.  
  3. Public Sub SetLabelText(ByVal LabelObject As Label, ByVal Value As String)
  4.  
  5.   If LabelObject.InvokeRequired Then
  6.     Dim dlg As New SetLabelTextDelegate(AddressOf SetLabelText)
  7.     dlg.Invoke(LabelObject, Value)
  8.   Else
  9.     LabelObject.Text = Value
  10.   End If
  11.  
  12. End Sub
C#
Expand|Select|Wrap|Line Numbers
  1. public delegate void SetLabelTextDelegate(Label LabelObject, string Value);
  2.  
  3. public void SetLabelText(Label LabelObject, string Value) 
  4.     if (LabelObject.InvokeRequired) { 
  5.         SetLabelTextDelegate dlg = new SetLabelTextDelegate(SetLabelText); 
  6.         dlg.Invoke(LabelObject, Value); 
  7.     } 
  8.     else { 
  9.         LabelObject.Text = Value; 
  10.     } 
  11. }
So where you would normally make a call like Label1.Text = "Stuff", you would now make the following call: SetLabelText(MyLabelObject, "Text to put in my label").

This will now post text from the secondary thread properly... in my applications, I usually create this pattern for each type of object I have on screen that will required updating from any thread and all calls to set the values will go through the SetxxxValue method (regardless of which thread I'm posting from, for consistency), which will pass the object to be updated and the text to put into it.
Nov 11 '08 #3
kspiros
16
Thanks guys i solve it with this
Expand|Select|Wrap|Line Numbers
  1.  
  2. //Declare a new delegate specific to updating the text of a label         
  3. delegate void UpdateLabelTextDelegate(Label lbl, string text);
  4.           //This method will invoke the delegate and pass our args along         
  5. void UpdateLabelText(Label lbl, string text)        
  6.  {             
  7. UpdateLabelTextDelegate dlg = new UpdateLabelTextDelegate(UpdateLabelTextByDelegate);             
  8. //Invoke this method on the control's original that owns the label's //handle             
  9. lbl.Invoke(dlg, new object[] { lbl, text });        
  10.  }         
  11.  //This method is invoked by our delegate and actually updates the label //text         
  12. void UpdateLabelTextByDelegate(Label lbl, string text)        
  13.  {            
  14.  lbl.Text = text;         
  15. }
Thanks a lot!!!
Nov 12 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Matt | last post by:
I want to change the label on the fly (when the user click the checkbox), but my attempt InputForm.lataLbl.value = "LATA * "; didn't work. Here's my code, any ideas? thanks !!! <html>...
5
by: AFN | last post by:
I'm trying to set a submit button to change text and color when clicked. Like saying "please wait" instead of "submit" and then changing the background color and text color. All works, except for...
3
by: OutdoorGuy | last post by:
Greetings, I have a very "novice" question to ask, so hopefully it will be easy. At any rate, I have a label on a Windows form called "label1". I simply want to change the "backcolor" property...
1
by: Shapper | last post by:
Hello, I have a label tag and a input tag in my page: <label for="subject">Nome:<input type="text" id="sbj"></input></label> How can I change the label value "Nome:" to "Name:" in Page_Load?...
2
by: Simon | last post by:
Dear reader, Is it possible to change the text of a label in a form on the event "By open". In the event some VBA code will change the label text.
4
by: darnnews | last post by:
When a user selects a "Medium" in a form I have made, I would like to change a Label on the form. Can I change a label based on an update of a listbox? This code does not work: Case "Print"...
5
by: Yohan Blurp | last post by:
Hi, Here is sample page to show you the problem : <html><body> <form action="/cgi-bin/test.cgi" method="post"> Data Path : <input type="text" size="50" value="C:\Test Data\May 2007.xls"...
3
by: bhanubalaji | last post by:
hi, I am unable to disable the text(label) in javascript..it's working fine with IE,but i am using MOZILLA.. can any one help regarding this.. What's the wrong with my code? I am...
1
by: kenneth6 | last post by:
Windows form app: 1. how to get text label updated? 2. how to get text label assigned with a variable value (integer / string)?
18
by: wizdom | last post by:
Help - change text on click - text has another onclick inside with php variables ---------- I think what I'm trying to do is simple. I have a 2 buttons on a page. 1 button allows a thread of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.