The awaitable below does not complete at the await
point and it does not capture the UI context. It means the UI modifying code that follows will be invoked in another thread (thread pool thread in this case).
private async void Button1_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append($"{Thread.CurrentThread.ManagedThreadId}, ");
Task t = Task.Delay(1000);
await t.ConfigureAwait(false);
sb.Append($"{Thread.CurrentThread.ManagedThreadId}");
Text = sb.ToString();
}
The code above runs without any problem. No error at runtime.
Question
Why is it allowed to modify UI components in non-UI thread? Is there something wrong with my understanding?