I'm reallt trying to get my head around async/await and Tasks. I'm trying to get one method down so that I can use it throughout my program. Basically I have my own version of BusyIndicator that I want to display whilst work is being done. Here is a basic example;
private async void OnPageLoad(object sender, RoutedEventArgs e)
{
var waitWindow = new PleaseWaitWindow();
waitWindow.Show();
await LoadCompanyContracts();
waitWindow.Close();
}
private async Task LoadCompanyContracts()
{
await Task.Run(() =>
{
Dispatcher.Invoke(() =>
{
//Work done here
});
});
}
Even through these two methods and attempting implementing Tasks my BusyIndicator still doesn't rotate, it is displayed but is frozen so in effect I believe the UI thread is still blocked. How can I modify this piece of code to ensure that all CPU bound work is not blocking the UI thread?
awaitcannot work on a method that returns void.Dispatcher.Invokeis a fancy way of saying "please get the code inside to run on the UI thread" - so you're just doing a complicated dance and then throwing away all of that work.