In the code, while the task is awaited in DoThis(), the context is captured and its a UI thread context. And the rest of the method will be resumed in the same context as I didn't use Configure.Await(false).
I want to know is what does it mean by "captured context"? Does it mean the state of thread - like its registers and stack - is saved somewhere in the memory and as soon as the async operation completes the rest of the method will be resumed using the register n stack values that were saved? If so, then does that mean UI thread would have to switch to that captured context to resume that method. And then switch back to the context again in btn_Click once DoAsync is completed? That way UI thread seems like handling 2 contexts, one at a time.. I'm confused!
Code
public static async Task DoAsync()
{
// executed in UI thread
....
....
Task t1 = await DoThis(uri); // async operation executed in Thread Pool thread
....
msg.Text = "Work Accomplished"; // UI Control can only be accessed in UI thread
}
}
protected async void btn_Click(object sender, EventArgs e)
{
// Since we asynchronously wait, the UI thread is not blocked.
// ** This allows the thread to handle other requests while we are waiting. **
await DoAsync();
....
....
}
I have read a few posts on this subject but I'm finding it hard to grasp the concept behind as they are explained in a more difficult way. I want someone to explain in more easy terms the intricacies involved in getting it done.