Refresh UI from background theme

This is just a curious question. Which one is the best way to update an interface from another thread. The first is:

private delegate void MyDelegateMethod(); void MyMethod() { if (unknowncontrol.InvokeRequired) { this.BeginInvoke(new MyDelegateMethod(MyMethod)); return; } unknowncontrol.property = "updating!"; } 

On the other hand:

 Invoke((System.Threading.ThreadStart)delegate() { unknowncontrol.property = "updating!"; }); 

Or is there a better way to do this?

Of course, this is for WinForms, for WPF there is a dispatcher. How is the code for WPF?

I ask, because in the past I experienced errors when updating the user interface from the raised event, using both of the above options. Such an error as: "no source code." I guess we all saw them: D.

Thank you and have a nice day!

0
source share
6 answers

Post on the Os Osherove Blog: http://osherove.com/blog/2006/3/1/the-3-ways-to-create-a-thread-safe-gui-with-net-20-with-one .html

 delegate void Func<T>(T t); Func del = delegate { // UI Code goes here }; Invoke(del); 
+1
source

I usually used the first model, but this is only because I found it clearer. In fact, there will be no effective difference between the two.

0
source

As I see it, the best way is to set the CLR property to which the ui-element property is bound.

0
source

The first method (BeginInvoke) ensures that the UI update code runs in the same thread that the control created. The second method does not. Having the entire executable user interface update code in a single thread avoids multithreaded problems and allows you to use controls that are not necessarily thread safe.

0
source

The default action delegate worked in 90% of cases:

 private void Log(String value) { // Verify that we're on the same thread if (textBox1.InvokeRequired) { // We're not on the same thread - Invoke the UI thread textBox1.Invoke(new Action<string>(Log), value); return; } // We're on the same thread - Update UI textBox1.Text += value + "\r\n"; } private void OnSomethingHappened() { Log("Something happened!"); } 
0
source

Use [Dispatcher.Invoke (DispatcherPriority, Delegate)] to change the user interface from another stream or from the background.

Step 1 Use the following namespaces

 using System.Windows; using System.Threading; using System.Windows.Threading; 

Step 2 Put the following line where you need to update the interface

 Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { //Update UI here })); 

Syntax

 [BrowsableAttribute(false)] public object Invoke( DispatcherPriority priority, Delegate method ) 

Parameters

priority

Type: System.Windows.Threading.DispatcherPriority

Priority regarding other pending operations in the Dispatcher Event Queue, the specified method is called.

method

Type: System.Delegate

A delegate to a method that does not accept arguments that are clicked on the dispatcher event queue.

Return value

Type: System.Object

The return value of the called delegate, or null if the delegate does not have a return value.

Version Information

Available with .NET Framework 3.0

0
source

All Articles