469,271 Members | 997 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

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 9674
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

Post your reply

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

Similar topics

3 posts views Thread by Matt | last post: by
3 posts views Thread by OutdoorGuy | last post: by
5 posts views Thread by Yohan Blurp | last post: by
1 post views Thread by kenneth6 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.