Task的使用

时间:2021-06-30 13:02:27

https://msdn.microsoft.com/en-us/library/dd321424(v=vs.110).aspx

Represents an asynchronous operation that can return a value.

Type Parameters

TResult

The type of the result produced by this Task<TResult>.

Task<TResult>.Result 属性

Gets the result value of this Task<TResult>.

public TResult Result { get; }

The result value of this Task<TResult>, which is the same type as the task's type parameter.

Accessing the property's get accessor blocks the calling thread until the asynchronous operation is complete; it is equivalent to calling the Wait method.

尝试获取Result会阻塞调用线程,直到异步操作完成。这等价于调用Wait方法。

Once the result of an operation is available, it is stored and is returned immediately on subsequent calls to the Result property.

Note that, if an exception occurred during the operation of the task, or if the task has been cancelled, the Result property does not return a value.

Instead, attempting to access the property value throws an AggregateException exception.