How can I prevent synchronous continuations on a Task?

后端 未结 6 471
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 11:04

I have some library (socket networking) code that provides a Task-based API for pending responses to requests, based on TaskCompletionSource

6条回答
  •  忘掉有多难
    2020-11-27 11:18

    What about instead of doing

    var task = source.Task;
    

    you do this instead

    var task = source.Task.ContinueWith( x => x.Result );
    

    Thus you are always adding one continuation which will be executed asynchronously and then it doesn't matter if the subscribers want a continuation in the same context. It's sort of currying the task, isn't it?

提交回复
热议问题