Action (() => {}) through the dispatcher measures ui
I tried a simple example of asynchronous await with and without .ConfigureAwait(false)
.
With the help .ConfigureAwait(false)
you can update the ui with the dispatcher, which is unchanged. This is case 1 and 3 in the code below - it works and I can figure out how it works.
My question is about case 2, where I add - completely unnecessary - update Action(() => { })
via the dispatcher.
This sometimes freezes my ui. Especially after calling the event handler again.
Can anyone explain why 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
}); }));
}
+3
source to share