I thought one of the points about async/await is that when the task completes, the continuation is run on the same context when the await was called, which would, in my case, be the UI thread.
So for example:
Debug.WriteLine("2: Thread ID: " + Thread.CurrentThread.ManagedThreadId); await fs.ReadAsync(data, 0, (int)fs.Length); Debug.WriteLine("3: Thread ID: " + Thread.CurrentThread.ManagedThreadId);
I would NOT expect this:
2: Thread ID: 10 3: Thread ID: 11
What gives? Why is the thread ID for the continuation different than the UI thread?
According to this article[^] I would need to explicitly call ConfigureAwait to change the behavior of the continuation context!
The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread. The method runs on the current synchronization context and uses time on the thread only when the method is active.
Asynchronous MethodsWhen the executing thread reaches an await expression, it hits the “pause” button and the method execution is suspended.
When the await keyword is used, the calling method is suspended and control is returned to the caller until the awaited task is completed. The await keyword can only be used within an async method.
The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.
When you await
, by default the await
operator will capture the current "context" and use that to resume the async
method.
This "context" is SynchronizationContext.Current
unless it is null
, in which case it is TaskScheduler.Current
. (If there is no currently-running task, then TaskScheduler.Current
is the same as TaskScheduler.Default
, the thread pool task scheduler).
It's important to note that a SynchronizationContext
or TaskScheduler
does not necessarily imply a particular thread. A UI SynchronizationContext
will schedule work to the UI thread; but the ASP.NET SynchronizationContext
will not schedule work to a particular thread.
I suspect that the cause of your problem is that you are invoking the async
code too early. When an application starts, it just has a plain old regular thread. That thread only becomes the UI thread when it does something like Application.Run
.
The await
expression will use the value of SynchronizationContext.Current
to return control flow back to the thread on which it occurred. In cases where this is null
it will default to the TaskScheduler.Current
. The implementation relies solely on this value to change the thread context when the Task
value completes. It sounds like in this case the await
is capturing a context that isn't bound to the UI thread
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With