Reputation: 10721
I'm using Stephen Toub's StaTaskScheduler
for the reason described here.
As far as I understand, task scheduling is the responsibility of Task Scheduler, while await
continuation is the responsibility of Synchronization Context (please correct me if I'm wrong).
Now I want to use await
inside my task code running on one of the STA threads, created by StaTaskScheduler
. Should I install a custom Synchronization Context, to make sure the code execution will be continued on the same STA thread after await
?
Upvotes: 3
Views: 896
Reputation: 457217
I cover the await
behavior in my intro to async
blog post.
By default, await
will capture the current SynchronizationContext
unless it is null
, in which case it will capture the current TaskScheduler
.
So, the answer is no, you do not need to install a custom SynchronizationContext
. When used inside a task executed by StaTaskScheduler
, await
will by default capture that task scheduler and resume on another task executed by that StaTaskScheduler
.
Note that StaTaskScheduler
does maintain a collection of STA threads, and the continuation may execute on any of those threads. If this is unacceptable, you'll need to limit the StaTaskScheduler
to a single thread (by passing 1
to its constructor).
Upvotes: 5