I am trying to update the UI after await. It was working good until I open a form before the Task.Delay, then I see that the thread id has changed.

Can anyone tell what's wrong here?

private async void TestTaskThread()
{       
    this.Text += " : " + Thread.CurrentThread.ManagedThreadId.ToString();

    Form2 form2 = new Form2();
    form2.ShowDialog();
    await Task.Delay(7);
    this.Text += " : " + Thread.CurrentThread.ManagedThreadId.ToString();
}

1 Answers

0
Community On

I can't replicate it. I added a button and on click I call the TestTaskThread method. The thread Id remains 1.

private async void btnShowDialogFromAsync_Click(object sender, EventArgs e)
{
    await TestTaskThread();
}

I also changed the return value of the method to Task.

The only way to replicate it is by configuring the await to skip restoring the context.

await Task.Delay(7).ConfigureAwait(false);