2

Possible Duplicate:
How to update GUI from another thread in C#?

I need to create C# application, which can change values of UI items in different threads. So, how can I do it? I try to create simple Thread object, and change TextView text in it, but I have got exception. How can I do it? Thank you.

Community
  • 1
  • 1
user1023177
  • 641
  • 3
  • 10
  • 18

2 Answers2

1

If this is a WinForms application you should use the Invoke/BeginInvoke method to marshal the call on the UI thread. And if you are using WPF/Silverlight you should use the Dispatcher.BeginInvoke method.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I use Invoke(new MethodInvoker(enctyptThread)). But my UI is frozen, and it is not good solution – user1023177 Nov 09 '11 at 13:42
  • @user1023177, where are you using this? Normally this should be done from the callback which executes on the background thread. – Darin Dimitrov Nov 09 '11 at 13:45
  • I have callback event (button1_click) - I execute new MethodInvoker(enctyptThread) here. – user1023177 Nov 09 '11 at 13:52
  • @user1023177, you should execute this in a new thread, not inside click event handlers. Click event handlers run on the UI thread. You should start a new thread there. – Darin Dimitrov Nov 09 '11 at 13:56
  • Please, can you create a new post with sample of code? It is very important for me, thank you. – user1023177 Nov 09 '11 at 13:59
  • @user1023177, there are many examples out there. Suffice to search and read a little: http://www.codeproject.com/KB/threads/Threading.aspx The article I have posted in my answer also contains examples. Don't hesitate to ask if you encounter some problems when you try to implement it. – Darin Dimitrov Nov 09 '11 at 14:03
1

You need to use Invoke to do it.

DaveShaw
  • 52,123
  • 16
  • 112
  • 141