Click here to Skip to main content
15,892,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing a C# program in which I try implying the use of threads. There is a label on a status bar that I would like to set a message based on the workings and the status of the current thread.
But anytime i try setting the text property of the control (label) in one of the threads other than the main thread that created it, I get the error message that i can not set the properties of the control in a thread whose ID is different from the thread that created the control (label).
I try using the
C#
invokeRequired()
method only to found out that the ToolStripLabel does not have any method with that name.
I will be glad if some one there can help me urgently. Thank you!
Posted
Updated 2-Jan-12 6:52am
v2

You can update the UI controls only in the UI thread. Have a look at this. There's a good example also: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx[^]
 
Share this answer
 
Comments
Espen Harlinn 2-Jan-12 12:54pm    
About what needs to be said on the topic - my 5
Wendelius 2-Jan-12 13:03pm    
Thank you :)
When I started using .NET I made the assumption that any UI element that required 'Invoking' from a thread would implement the InvokeRequired property. However this is not true and there are some lightweight UI elements such as Toolstrip items and TreeView nodes which are not derived from the Control class but still should only be updated from the UI thread.

So what should be done if there is no Invoke method? Well the answer is to use the Invoke/InvokeRequired of any control present on the host form, even the form itself.

C#
private delegate void LabelUpdateDelegate(String text);

private void UpdateStatusLabel(String text) {
  if (this.InvokeRequired) {
    this.Invoke(new LabelUpdateDelegate(UpdateStatusLabel), text);
  } else {
    status.Text = text;
  }
}


Alan.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900