How do I chain Asynchronous Operations with the Task Parallel library in .NET 4?

后端 未结 3 1508
长发绾君心
长发绾君心 2021-02-02 14:07

I\'m attempting to programmatically chain asynchronous operations in C#4, such as Writes to a given Stream object. I originally did this \"manually\", hooking callbacks from one

3条回答
  •  自闭症患者
    2021-02-02 14:37

    My best idea so far is to chain the creation of the new write task, then use the Unwrap extension method to turn Task back into Task:

    public static Task ChainWrite(Stream stream, byte[] data, Task precedingTask)
    {
        return precedingTask.ContinueWith(x => CreateWriteTask(stream, data)).Unwrap();
    }
    

提交回复
热议问题