Home >Backend Development >C++ >Await vs. Result in .NET Asynchronous Programming: Why Does Using 'Result' Lead to Deadlocks?
The difference between and await
: Why use Result
to cause dead locks? Result
In the .NET asynchronous programming, understanding the difference between
is very important. await
Allow collaborative multi -task processing, and Result
may cause dead locks. await
Result
obstruction await
Result
The test program is hung in the example because it tries to use
result twice simultaneously. This can cause dead locks because the underlying HTTP request is blocked when waiting for response. CheckStatusTwiceResultTest
Result
CheckStatus()
The advantages of
The keyword allows the execution of the pause method, and the execution is resumed after the asynchronous operation is completed. In this example, will not hang when runtime, because await
Keywords will be controlled to other tasks when waiting for HTTP response.
await
CheckStatusAwaitTest
Extension method Create a await
, and use the asynchronous commission to set its results. However, calling
hanging. Avoid dead locks
ExecuteTaskAsync
To avoid dead locks, the results of TaskCompletionSource
obtaining asynchronous operations are always used. The best practice is to avoid calling Result
on the task, especially when performing in UI threads or other sensitive environments. CheckStatusTwiceResultTest
Use the execution
method in the code. For asynchronous methods, this is not recommended, because await
will allocate a new thread. Instead, the inherent asynchronousness provided by the .NET framework should be used, and the method should be set to asynchronous methods. Result
The above is the detailed content of Await vs. Result in .NET Asynchronous Programming: Why Does Using 'Result' Lead to Deadlocks?. For more information, please follow other related articles on the PHP Chinese website!