小编Jon*_*son的帖子

从TaskCompletionSource返回Task而不是Task <TResult>

正如我在几个编码示例中看到的,以及从这个SO问题我可以理解的,我应该能够从TaskCompletionSource返回一个非泛型任务

(i.e., Return Task and not Task<TResult> from the method UploadFilesAsync) 
Run Code Online (Sandbox Code Playgroud)

但是以下代码:

public async Task UploadFilesAsync(string fileAPath, string fileBPath)
{
   var tcs = new TaskCompletionSource<Object>();

   //logic to process files

   try
   {
      await Task.WhenAll(uploadFileAAsync(fileAPath), 
                         uploadFileBAsync(fileBPath));
      tcs.TrySetResult(null);
   }
   catch (Exception e)
   {
      tcs.SetException(e);
   }
   finally
   {
      //logic to clean up files
   }
   return tcs.Task;
}
Run Code Online (Sandbox Code Playgroud)

产生以下语法错误

'UploadFilesAsync(string, string)' is an async method that returns 'Task', 
a return keyword must not be followed by an object expression. 
Did you …
Run Code Online (Sandbox Code Playgroud)

.net c# task-parallel-library async-await

15
推荐指数
1
解决办法
2万
查看次数

标签 统计

.net ×1

async-await ×1

c# ×1

task-parallel-library ×1