Home >Backend Development >C++ >Is `Task.GetAwaiter().GetResult()` a Better Alternative to `Task.Result` for Synchronous Blocking?
Is it equivalent to ? Task.GetAwaiter().GetResult()
Task.Result
In asynchronous programming, sometimes the asynchronous method is required to perform simultaneously. One method is to use , but this may lead to exhaustion of dead locks and thread pool resources. However, it is considered to be better than and , because it spreads abnormalities directly, rather than packaging it in
Task.GetAwaiter().GetResult()
Task.Wait
, Task.Result
and AggregateException
The differences
and Task.GetAwaiter().GetResult()
will automatically pack abnormal packaging in Task.Wait
, this is to maintain compatibility with the early frame version. In contrast, directly spreads abnormalities and avoids packaging problems. Task.Result
Task.Wait
Task.Result
Microsoft PFX team said AggregateException
allow developers to directly access the abnormal communication logic used by Task.GetAwaiter().GetResult()
operators. This is very useful in the scene that needs to be retained without packaging.
Expert Stephen Cleary further emphasized the clarity of . He explained that it clearly stated the wrong inspection and suggested to use it as the preferred method for synchronous obstruction under certain circumstances. Task.GetAwaiter().GetResult()
Warning:
task.GetAwaiter().GetResult()
Although has obvious advantages, it should avoid relying on it, await
, and
The above is the detailed content of Is `Task.GetAwaiter().GetResult()` a Better Alternative to `Task.Result` for Synchronous Blocking?. For more information, please follow other related articles on the PHP Chinese website!