C# WPF Async

[linkstandalone]

So recently i was working on a very resource heavy function in C#, and while the function ran, it completely halted the UI (WPF), and refused to make any updates to the UI while it was running.

So i decided to make the function Async, since that would surely fix the problem? Well no, not quite. With the function Async it completely refused to run, the reason for this is that WPF UI elements can't just be changed by something on another thread, so to solve this i used the Dispathcher function.


Dispatcher.Invoke(() => { someLabel.Content = "Not Running"; someLabel.Foreground = Media.Brushes.Red; someProgressBar.Value = 0; });

The function is a simple void, but is executed with Task.Run(() => SomeVoid() ); To give the asynchronous functionality, so the program will be running smooth and with a progress bar.

Do note that when running on another thread, you will not only need Dispatcher to change values, but also to read them.