More about asynchronous programming in .NET
There are two types of scenarios where async code is usually introduced:
- I/O-bound operations: These involve resources fetched from the network or disk.
- CPU-bound operations: These are in-memory, CPU-intensive operations.
In this section, we will create some real-world examples that use async
and await
for each type of operation. Whether you are waiting for an external process to complete or performing CPU-intensive operations within your application, you can leverage asynchronous code to improve your application’s performance.
Let’s start by looking at some examples of I/O-bound operations.
I/O-bound operations
When you are working with I/O-bound code that is constrained by file or network operations, your code should use async
and await
to wait for the operations to complete.
The .NET methods to perform network and file I/O are asynchronous, so the use of Task.Run
will not be necessary: