You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/csharp/asynchronous-programming/async-scenarios.md
+68Lines changed: 68 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -208,6 +208,74 @@ Avoid writing code that depends on the state of global objects or the execution
208
208
209
209
A recommended goal is to achieve complete or near-complete [Referential Transparency](https://en.wikipedia.org/wiki/Referential_transparency) in your code. This approach results in a predictable, testable, and maintainable codebase.
210
210
211
+
### Synchronous access to asynchronous operations
212
+
213
+
In scenarios, you might need to block on asynchronous operations when the `await` keyword isn't available throughout your call stack. This situation occurs in legacy codebases or when integrating asynchronous methods into synchronous APIs that can't be changed.
214
+
215
+
> [!WARNING]
216
+
> Synchronous blocking on asynchronous operations can lead to deadlocks and should be avoided whenever possible. The preferred solution is to use `async`/`await` throughout your call stack.
217
+
218
+
When you must block synchronously on a `Task`, here are the available approaches, listed from most to least preferred:
- Executes the asynchronous method on a thread pool thread.
252
+
- Can help avoid some deadlock scenarios.
253
+
- Adds overhead by scheduling work to the thread pool.
254
+
255
+
#### Use Wait() and Result
256
+
257
+
You can use a blocking approach by calling <xref:System.Threading.Tasks.Task.Wait> and <xref:System.Threading.Tasks.Task`1.Result>. However, this approach is discouraged because it wraps exceptions in <xref:System.AggregateException>.
258
+
259
+
```csharp
260
+
Task<string>task=GetDataAsync();
261
+
task.Wait();
262
+
stringresult=task.Result;
263
+
```
264
+
265
+
Problems with `Wait()` and `Result`:
266
+
267
+
- Exceptions are wrapped in `AggregateException`, making error handling more complex.
268
+
- Higher deadlock risk.
269
+
- Less clear intent in code.
270
+
271
+
#### Additional considerations
272
+
273
+
- Deadlock prevention: Be especially careful in UI applications or when using a synchronization context.
- Exception handling: Test error scenarios carefully as exception behavior differs between patterns.
276
+
277
+
For more detailed guidance on the challenges and considerations of synchronous wrappers for asynchronous methods, see [Should I expose synchronous wrappers for asynchronous methods?](https://devblogs.microsoft.com/pfxteam/should-i-expose-synchronous-wrappers-for-asynchronous-methods/).
278
+
211
279
## Review the complete example
212
280
213
281
The following code represents the complete example, which is available in the *Program.cs* example file.
0 commit comments