开发者

Accessing components from a different thread C#

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.开发者_Go百科0 (I have to...).


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#?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜