C # GUI application, another class from another thread updating the user interface

I’m learning how to do this for a week, and I'm still not sure of the right approach, in some examples I see that the Thread class is used in others, I see that Invoke is used, which confused me the bet.

I have a GUI program in C # that contains a text box that will be used to provide information to the user.

The problem I am facing is that I'm not sure how to add text to a textBox from another class that works in another thread. If someone can show me a working example, it will help me a lot.

Best wishes!

+4
source share
3 answers

Easy:

MainWindow.myInstance.Dispatcher.BeginInvoke(new Action(delegate() {MainWindow.myInstance.myTextBox.Text = "some text";}); 

WHERE MainWindow.myInstance is the public static variable set for the MainWindow instance (must be set in the constructor and will be null until an instance is created).

It’s good that a lot on one line allows me to go through it:

If you want to update the user interface control, you, as you say, must do this from the user interface thread. There is a built-in way to pass a delegate (method) to the user interface stream: Dispatcher. I used MainWindow.myInstance, which (like all user interface components) contains a link to the Manager - you can also save the link to the Manager in your variable:

 Dispatcher uiDispatcher = MainWindow.myInstance.Dispatcher; 

Once you have the dispatcher, you can either Invoke () BeginInvoke () pass the delegate to run in the user interface thread. The only difference is that Invoke () will only return after the delegate is started (i.e., in your case, TextBox Text was installed), while BeginInvoke () will return immediately, so your other thread you're calling from is can continue (Dispatcher will start your delegate soon, as it can probably be anyway).

I passed the anonymous delegate above:

 delegate() {myTextBox.Text = "some text";} 

The bit between {} is the method block. This is called anonymous because only one is created and it does not have a name, but I could create an instance of the delegate:

 Action myDelegate = new Action(UpdateTextMethod); void UpdateTextMethod() { myTextBox.Text = "new text"; } 

Then passed this:

 uiDispatcher.Invoke(myDelegate); 

I also used the Action class, which is a built-in delegate, but you could create your own - you can learn more about delegates on MSDN, as this is a little off topic.

+8
source

It sounds like you're using a background thread for processing, but want the user interface to respond? BackgroundWorker sounds like a ticket:

The BackgroundWorker class allows you to start operations on a separate, dedicated thread. Painstaking operations such as loading and a transaction database can cause the user interface (UI) to look as if it has stopped responding while they are running. When you need a responsive interface and are faced with long delays associated with such operations, the BackgroundWorker class provides a convenient solution.

0
source

Just use BackgroundWorker for this. It just takes away the pain of managing your flows. for more information you can see: http://dotnetperls.com/backgroundworker

0
source

All Articles