0

I know when we call an async method it frees the UI thread and suspends the execution of the thread when it encounters the await keyword. Now suppose we are returning somevalue from the awaited method like:-

public async Task SomeEvent()
{
   x= await Sum(2, 5); //Returns some output value
   TextBox.Text = x; // updates the result in the UI 
}

Now as per my understanding, since the UI thread returns back to the method which called this "SomeEvent" when it encounters the await keyword and It is necessary to perform all the UI related operations on the main thread. Now in the above program, UI is busy in executing some other operations as it is freed from executing the above method and when asynchronously the execution of the awaited process completes then how the UI will get updated.

Bhupesh
  • 35
  • 7
  • There is a really decent and comprehensive answer to your question here: https://stackoverflow.com/a/37419845/4393579 – Stas Ivanov Aug 18 '17 at 08:25

1 Answers1

2

There is such thing as SynchronizationContext. Simply saying, the thread that completed some async work will wait (will be put into a queue) for the UI context to be free again, to run in it. It looks like this happens instantly, but actually it does not.

cassandrad
  • 3,412
  • 26
  • 50