Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwingUtilities.invokeLater equivalent in WPF

Tags:

java

c#

wpf

In WPF UI elements updates' are allowed only on the UI dispatcher thread. Hence, trying to update a UI element from a separate thread will end up in an exception. The only way I found on the internet to get around this is to have something like:

ThreadStart start = delegate(){
    Dispatcher.Invoke(DispatcherPriority.Normal, new Action...
};
new Thread(start).Start();

Java has the SwingUtilities.invokeLater method to do the same kind of thing.

My question is: Is there a specialized WPF utility class with an equivalent of the Java SWING SwingUtilities.invokeLater?

like image 397
GETah Avatar asked Mar 26 '26 23:03

GETah


2 Answers

Dispatcher.BeginInvoke(
   new Action(() => {
      label.Text = "Foo"
   })
);

might be more closer to the invokeLater syntax

like image 129
parapura rajkumar Avatar answered Mar 29 '26 11:03

parapura rajkumar


I'm pretty sure the answer is: no there isn't (other than Dispatcher, which I think will help you a lot).

A comment on your code: the threading isn't necessary. Just use

Dispatcher.Invoke(...)
like image 31
Kendall Frey Avatar answered Mar 29 '26 13:03

Kendall Frey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!