You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Overview

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:


Synchronous and Asynchronous

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:

  • Responsiveness: Keeps applications responsive, especially in UI scenarios where blocking the main thread can lead to a frozen interface.
  • Scalability: Efficiently handles multiple concurrent operations without the overhead of creating new threads for each task.
  • Resource Efficiency: Frees up threads to perform other work while awaiting long-running operations, optimizing CPU and memory usage.

Async and Await

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:

  • Method declaration: You use async to modify a method, indicating that it must return a Task, Task<T>, or void (in specific cases, such as event handlers).
  • Enables the use of await: Without async, the compiler does not allow the use of await inside the method.
  • Return management: A method with 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.

Code example
public async Task<int> GetDataAsync()
{
    int result = await SomeOtherAsyncMethod();
    return result;
}

Controlling Tasks Without Await

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.

Code example
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.


Best Practices

  • Avoid Blocking Calls: Do not use blocking calls like Task.Wait() or Task.Result, which negate the benefits of asynchronous programming.
  • Async All the Way: Ensure that asynchronous methods call other asynchronous methods to maintain non-blocking behavior throughout the call stack.
  • Exception Handling: Use try-catch blocks within async methods to handle exceptions properly.
  • Cancellation Tokens: Implement cancellation tokens to allow operations to be canceled if they are no longer needed.
  • Progress Reporting: Utilize progress reporting mechanisms to provide feedback during long-running operations.

In this section:

The root page @parent could not be found in space v10.

  • No labels