相关疑难解决方法(0)

没有TaskCompletionSource的任务链接?

我正在将一些异步/等待代码转换为链式任务,因此我可以在已发布的框架中使用它.等待代码看起来像这样

public async Task<TraumMessage> Get() {
  var message = await Invoke("GET");
  var memorized = await message.Memorize();
  return memorized;
}
Run Code Online (Sandbox Code Playgroud)

哪里

Task<TraumMessage> Invoke(string verb) {}
Task<TraumMessage> Memorize() {}
Run Code Online (Sandbox Code Playgroud)

我希望链接InvokeMemorize返回由此产生的任务Memorize,但结果是Task<Task<TraumMessage>.我最终得到的解决方案是TaskCompletionSource<TraumMessage>我的信号:

public Task<TraumMessage> Get() {
  var completion = new TaskCompletionSource<TraumMessage>();
  Invoke("GET").ContinueWith( t1 => {
     if(t1.IsFaulted) {
       completion.SetException(t1.Exception);
       return;
     }
     t1.Result.Memorize().ContinueWith( t2 => {
       if(t2.IsFaulted) {
         completion.SetException(t2.Exception);
         return;
       }
       completion.SetResult(t2.Result);
     });
  });
  return completion.Task;
}
Run Code Online (Sandbox Code Playgroud)

有没有办法完成这个没有TaskCompletionSource

c# task-parallel-library async-ctp

5
推荐指数
1
解决办法
2344
查看次数

标签 统计

async-ctp ×1

c# ×1

task-parallel-library ×1