How to get a Task that uses SynchronizationContext? And how are SynchronizationContext used anyway?

后端 未结 2 1800
无人共我
无人共我 2020-12-08 20:09

I am still learning the whole Task-concept and TPL. From my current understanding, the SynchronizationContext functions (if present) are used by await to dispat

相关标签:
2条回答
  • 2020-12-08 20:22

    As you're learning this, it's important to point out that Task as used by the TPL is quite different than Task as used by async/await, even though they're the same type. For example, TPL commonly uses parent/child tasks, but async/await does not.

    TPL uses task schedulers to execute its tasks. As Dennis pointed out, TaskScheduler.FromCurrentSynchronizationContext will give you a task scheduler that uses Post on the current SynchronizationContext to execute its task.

    async/await usually does not use task schedulers. I have an introductory async/await post on my blog that includes context information, and I also mention it briefly in my MSDN article (it's easy to overlook, though). Essentially, when an async method suspends at an await, by default it will capture the current SynchronizationContext (unless it is null, in which case it will capture the current TaskScheduler). When the async method resumes, it resumes executing in that context.

    Dennis pointed out the TPL way of scheduling a task to the current SynchronizationContext, but in async/await world, that approach isn't necessary. Rather, you can explicitly schedule tasks to the thread pool via Task.Run:

    async Task MyMethodAsync()
    {
      // Whee, on a SynchronizationContext here!
      await Task.Run(() => { }); // Ooo, on the thread pool!
      // Back on the SynchronizationContext ...
      //  ... automagically!
    }
    

    I wrote my SynchronizationContext article precisely because the MSDN docs were so lacking. I have a little more information on my blog, but all the important bits are in the MSDN article. Many types use AsyncOperation rather than SynchronizationContext directly; the best documentation for this is buried under the EAP docs (section "Threading and Context"). But I should also point out that EAP is effectively obsolete due to async/await, so I wouldn't write code using AsyncOperation (or SynchronizationContext) - unless I was actually writing my own SynchronizationContext.

    0 讨论(0)
  • 2020-12-08 20:25

    How can I obtain a Task, that actually runs an action but is dispatched using SynchronizationContext.Current.Send/Post?

    Use special task scheduler:

    Task.Factory.StartNew(
        () => {}, // this will use current synchronization context
        CancellationToken.None, 
        TaskCreationOptions.None, 
        TaskScheduler.FromCurrentSynchronizationContext());
    

    And can anyone recommend a good introduction into SynchronizationContext

    Look at the article It's All About the SynchronizationContext by Stephen Cleary.

    0 讨论(0)
提交回复
热议问题