Asynchronous programming is a paradigm that allows for the execution of tasks without blocking the main execution thread. This approach is essential for developing stable and scalable applications, especially when dealing with operations that are I/O-bound or time-consuming, such as network requests, database operations, or file I/O.
On this page:
Programming allows running tasks one after another or running tasks without waiting for others to finish. When tasks run one after another, each task waits for the task before it finishes. When tasks run without waiting, tasks start and proceed without delay, allowing operations to occur at the same time.
Benefits:
Both work together to implement an asynchronous execution model. async prepares the method for asynchronous operations, while await manages the wait points within the method.
You apply the async
modifier to a method. It indicates that the function contains asynchronous operations and can use await
. It only affects the method or expression where you declare it.
Characteristics:
async
to modify a method, indicating that it must return a Task
, Task<T>
, or void
(in specific cases, such as event handlers).await
: Without async
, the compiler does not allow the use of await
inside the method.async
always returns a Task
or Task<T>
. If the method would originally return void
, the compiler transforms the return into a Task
or Task<T>
.The await
operator directly applies to a Task
or Task<T>
instance. It suspends the execution of the current method until the associated Task
completes. It only affects the line or block of code where you use it.
Characteristics:
Waiting for the completion of an asynchronous operation: It suspends execution at the point where it appears, freeing up control of the thread for other operations while it waits.
Asynchronous context: You can only use it in methods or expressions marked as async
.
Return of control: After the awaited operation completes, the method's execution flow continues from the point of the await
.
public async Task<int> GetDataAsync() { int result = await SomeOtherAsyncMethod(); return result; } |
You can control and monitor the status of an asynchronous operation without awaiting it immediately. This is useful when you want the program to continue executing other tasks while the asynchronous operation runs in the background.
Task taskName = FunctionAsync(); if (taskName.IsCompleted) { // Perform actions after the task has completed } |
By checking the IsCompleted
property of the task, you can determine if the asynchronous operation has finished and then execute additional code accordingly.
Task.Wait()
or Task.Result
, which negate the benefits of asynchronous programming.In this section: