I tried a simple async-await example with and without .ConfigureAwait(false)
.
With .ConfigureAwait(false)
you can update the ui via the dispatcher, which is unneccesary without it.
This is case 1 and 3 in the code below - that works and I can understand how it works.
My question is about case 2 where I add a - completely unneccesary -- refresh
Action(() => { })
via the dispatcher.
This occasionally freezes my ui. Especially after invoking the eventhandler repeatedly.
Can anybody explain why the ui freezes in case 2?
private void Test_Click(object sender, RoutedEventArgs e)
{
Test();
}
public async void Test()
{
Print("Start task");
// Case 1 & 2
await Task.Delay(2000);
// Case 3
await Task.Delay(2000).ConfigureAwait(false);
Print("Finished task");
}
void Print(string text)
{
// Case 1 & 2
Output.Inlines.Add(new Run(text) { Foreground = Brushes.Blue, FontWeight = FontWeights.Bold });
// Case 2 only
Output.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() => { }));
// Case 3
Output.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background,
new Action(() =>
{
Output.Inlines.Add(new Run(text) { Foreground = Brushes.Blue, FontWeight = FontWeights.Bold
}); }));
}