正如我在几个编码示例中看到的那样,以及我可以从这个SO 问题中理解的内容,我应该能够从 TaskCompletionSource 返回一个非泛型任务
(i.e., Return Task and not Task<TResult> from the method UploadFilesAsync)
然而下面的代码:
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;
}
产生以下语法错误
'UploadFilesAsync(string, string)' is an async method that returns 'Task',
a return keyword must not be followed by an object expression.
Did you intend to return 'Task<T>'?
我的目标是 .NET 4.5。我知道它可以返回 Task(of object) 但这会使 API 感觉“脏”。是返回 Task(of object) 还是可以返回 Task (代码中显示的非泛型)的首选做法?