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?
Dispatcher.BeginInvoke(
new Action(() => {
label.Text = "Foo"
})
);
might be more closer to the invokeLater syntax
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(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With