【发布时间】:2013-04-26 06:13:52
【问题描述】:
正如我在几个coding examples 中看到的,以及我可以从这个SO question 中理解的内容,我应该能够从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 (代码中显示的非泛型)?
【问题讨论】:
标签: c# .net task-parallel-library async-await