I checked the source code of TaskOfResult.cs
(Source code of TaskOfResult.cs):
If Task
is not completed, Task.Result
will call Task.Wait()
method in getter
.
public TResult Result
{
get
{
// If the result has not been calculated yet, wait for it.
if (!IsCompleted)
{
// We call NOCTD for two reasons:
// 1. If the task runs on another thread, then we definitely need to notify that thread-slipping is required.
// 2. If the task runs inline but takes some time to complete, it will suffer ThreadAbort with possible state corruption.
// - it is best to prevent this unless the user explicitly asks to view the value with thread-slipping enabled.
//#if !PFX_LEGACY_3_5
// Debugger.NotifyOfCrossThreadDependency();
//#endif
Wait();
}
// Throw an exception if appropriate.
ThrowIfExceptional(!m_resultWasSet);
// We shouldn't be here if the result has not been set.
Contract.Assert(m_resultWasSet, "Task<T>.Result getter: Expected result to have been set.");
return m_result;
}
internal set
{
Contract.Assert(m_valueSelector == null, "Task<T>.Result_set: m_valueSelector != null");
if (!TrySetResult(value))
{
throw new InvalidOperationException(Strings.TaskT_TransitionToFinal_AlreadyCompleted);
}
}
}
If We call GetAwaiter
method of Task
, Task
will wrapped TaskAwaiter<TResult>
(Source code of GetAwaiter()), (Source code of TaskAwaiter) :
public TaskAwaiter GetAwaiter()
{
return new TaskAwaiter(this);
}
And If We call GetResult()
method of TaskAwaiter<TResult>
, it will call Task.Result
property, that Task.Result
will call Wait()
method of Task
( Source code of GetResult()):
public TResult GetResult()
{
TaskAwaiter.ValidateEnd(m_task);
return m_task.Result;
}
It is source code of ValidateEnd(Task task)
( Source code of ValidateEnd(Task task) ):
internal static void ValidateEnd(Task task)
{
if (task.Status != TaskStatus.RanToCompletion)
HandleNonSuccess(task);
}
private static void HandleNonSuccess(Task task)
{
if (!task.IsCompleted)
{
try { task.Wait(); }
catch { }
}
if (task.Status != TaskStatus.RanToCompletion)
{
ThrowForNonSuccess(task);
}
}
This is my conclusion:
As can be seen GetResult()
is calling TaskAwaiter.ValidateEnd(...)
, therefore Task.Result
is not same GetAwaiter.GetResult()
.
I think GetAwaiter().GetResult()
is a beter choice instead of .Result
because it don't wrap exceptions.
I read this at page 582 in C# 7 in a Nutshell (Joseph Albahari & Ben Albahari) book
If an antecedent task faults, the exception is re-thrown when the
continuation code calls awaiter.GetResult()
. Rather than calling
GetResult
, we could simply access the Result property of the
antecedent. The benefit of calling GetResult
is that if the
antecedent faults, the exception is thrown directly without being
wrapped in AggregateException
, allowing for simpler and cleaner
catch blocks.
Source: C# 7 in a Nutshell's page 582
GetResult
: "This type and its members are intended for use by the compiler." Other person shouldn't be using it. – spenderasync
/await
method in MVC, for example) – Marc Gravell♦