等待任务 < T > 和任务 < T > 的区别是什么?

public async Task<string> GetName(int id)
{
Task<string> nameTask = Task.Factory.StartNew(() => string.Format("Name matching id {0} = Developer", id));
return nameTask.Result;
}

在上面的方法 return 语句中,我使用的是 Task<T>.Result属性。

public async Task<string> GetName(int id)
{
Task<string> nameTask = Task.Factory.StartNew(() => string.Format("Name matching id {0} = Developer", id));
return await nameTask;
}

在这里,我使用 await Task<T>。我不会错,如果我认为等待将释放调用线程,但 Task<T>.Result将阻止它,它是正确的吗?

15045 次浏览

I wont be wrong if I think that await will release the calling thread but Task.Result will block it, would it be right?

You're correct, as long as the task hasn't completed synchronously. If it did, using either Task.Result or await task will execute synchronously, as await will first check if the task has completed. Otherwise, if the task hasn't completed, it will block the calling thread for Task.Result, while using await will asynchronously wait for the tasks completion. Another thing that differs is exception handling. While the former will propagate an AggregationException (which may contain one or more exceptions), the latter will unwrap it and return the underlying exception.

As a side note, using asynchronous wrappers over sync methods is bad practice and should be avoided. Also, using Task.Result inside an async method is a cause for deadlocks and should also be avoided.

I wont be wrong if I think that await will release the calling thread but Task.Result will block it, would it be right?

Generally, yes. await task; will "yield" the current thread. task.Result will block the current thread. await is an asynchronous wait; Result is a blocking wait.

There's another more minor difference: if the task completes in a faulted state (i.e., with an exception), then await will (re-)raise that exception as-is, but Result will wrap the exception in an AggregateException.

As a side note, avoid Task.Factory.StartNew. It's almost never the correct method to use. If you need to execute work on a background thread, prefer Task.Run.

Both Result and StartNew are appropriate if you are doing dynamic task parallelism; otherwise, they should be avoided. Neither is appropriate if you are doing asynchronous programming.