正如我在几个编码示例中看到的,以及从这个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)