0

I have a windows form with a button in it. I have 2 threads and i want to change the button name from the other thread. I get an error when i do that. how can i change the button name?

P.S. I know that a same question alredy posted, but the solution there can't help me. I can't use the Dispatcher, maybe it's because i use .NET 2.0 (I have to...).

aharon
  • 7,393
  • 10
  • 38
  • 49

2 Answers2

1
delegate void MyDelegate(string x);

void ChangeName(string name)
{
   if (this.InvokeRequired)
   {
     this.Invoke(new MyDelegate(this.ChangeName), new object[]{name});
     return;
   }
   this.button.Text = name;
}

more info here How to update the GUI from another thread in C#?

Community
  • 1
  • 1
madmik3
  • 6,975
  • 3
  • 38
  • 60
0

you need to look at the:

InvokeRequired and 
Invoke

methods on the actual control. InvokeRequired will tell you if you need to do this stuff via invoke (i.e. it's being called from a thread that is not the UIThread) and invoke will perform the action.

it's not that different from Dispatcher except the responsibility falls to the control itself and not some other class.

peteisace
  • 2,754
  • 1
  • 19
  • 19