What is the difference between await Task<T> and Task<T>.Result?
What is the difference between await Task<T> and 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 nameTask.Result;
}
In above method return statement I am using the Task<T>.Result
property.
public async Task<string> GetName(int id)
{
Task<string> nameTask = Task.Factory.StartNew(() => string.Format("Name matching id {0} = Developer", id));
return await nameTask;
}
Here I am using await Task<T>
. I wont be wrong if I think that await will release the calling thread but Task<T>.Result
will block it, would it be right?
回答1
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.
回答2
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.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2021-07-05 The need for legislative reform on secrecy orders
2021-07-05 Can a foreign key be NULL and/or duplicate?
2019-07-05 The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.
2017-07-05 Create the Data Access Layer
2015-07-05 Leap Years
2015-07-05 Find the capitals
2015-07-05 Area of a Circle