async & await keyword in C#

In this article, we will discuss:

  • What is an async Method?
  • The Flow of Control in an Async Method
  • The await Expression
  • Exception Handling and the await Expression
  • Cancelling an async Operation
  • Waiting Asynchronously for Tasks in the async Method
  • Summary

 

What is an async Method?

An async method is a method that returns to the calling method before completing all its work, and then completes its work while the calling method continues its execution.

An async method has the following characteristics: More On CSharp

  • An async method must have the async keyword in its method header, and it must be before the return type.
  • This modifier doesn’t do anything more than signal that the method contains one or more await expressions.
  • It contains one or more await expressions. These expressions represent tasks that can be done asynchronously.
  • It must have one of the following three return types.
    − void :If the calling method just wants the async method to execute, but doesn’t need any further interaction with it
    − Task : If the calling method doesn’t need a return value from the async method, but needs to be able to check on the async method’s state
    − Task<T> :If the calling method is to receive a value of type T back from the call, the return type of the async method must be Task
  • An async method can have any number of formal parameters of any types but it cannot be out or ref parameters.
  • The name of an async method should end with the suffix Async.
  • Otherthan Methods, lambda expressions and anonymous methods can also act as async objects.

 

Using an async method that returns a Task<int> object:

Output:

 

Using an async method that returns a Task object:

Output:

Value: 21
Async stuff is done

Using an async method that returns a void object:

Output:

Value: 21
Program Exiting

The Flow of Control in an Async Method :

The body of an async method has divided mainly into three sections.

  • Before the first await expression : This includes all the code at the beginning of the method up until the first await expression. This section contains very minimal code that doesn’t require too much processing.
  • The await expression: This expression represents the task to be performed asynchronously.
  • Continuation: This is the rest of the code in the method, following the await expression. This includes the information about which thread it’s on, the values of the variables currently in scope, and other things it’ll need in order to resume execution later, after the await expression completes

The code in the async method does the following:

  • It executes, asynchronously, its await expression’s awaitable task.
  • When the await expression is done, it executes the continuation.
  • The continuation itself might have other await expressions, which are handled the same way. That is, the await expression is executed asynchronously, followed by the execution of its continuation.
  • When the continuation encounters a return statement or the end of the method:
    • If the method’s return type is void, the flow of control exits.
    • If the method’s return type is Task, the continuation sets the status properties on the Task and exits. If the return type is a Task<T>, the continuation additionally sets the Result property of the Task object.

 

The await Expression:

The await expression specifies a task to be done asynchronously.

Syntax:

There are now a number of BCL methods that return objects of type Task<T>, you’ll most likely have your own methods that you want to use as the task for an await expression. The easiest way to do that is to create a Task from your method using the Task.Run method.

the most important fact about the Task.Run method is that it runs your method on a different thread.
One signature of the Task.Run method is,it takes a Func delegate as a parameter.

So, to pass your method to the Task.Run method, you need to create a delegate from it.

There are three ways to do this.

In the code, method Get20 has a form compatible with a Func<int> delegate since it takes no parameters and returns an int.

  • In the first instance, which is in the first two lines of method DoWorkAsync, a Func delegate named twenty is created using Get20. That delegate is then used in the Task.Run method in the next line.
  • In the second instance, a Func delegate is created right in the Task.Run method’s parameter list.
  • The last instance doesn’t use the Get20 method at all. It uses the return statement that comprises the body of the Get20 method, and uses it as the body of a lambda expression compatible with a Func<int> delegate. The lambda expression is implicitly converted to the delegate.

 

 

Exception Handling and the await Expression:

You can use await expression inside atry statement.

Output:

Cancelling an async Operation :

You can cancel your own async operation.There are two classes in the System.Threading.Tasks namespace that are designed for this purpose: CancellationToken and CancellationTokenSource.

  • A CancellationToken object contains the information about whether a task should be cancelled or not.
  • A task that has a CancellationToken object needs to periodically inspect it to see what the token’s state is. If the CancellationToken object’s
    IsCancellationRequested property is set to true, the task should halt its operations and return.
  • A CancellationToken is nonreversible and can only be used once. That is, once it’s IsCancellationRequested property is set to true, it can’t be changed.
  • A CancellationTokenSource object creates a CancellationToken object, which can then be given to various tasks. Any objects holding a cancellationTokenSource can call its Cancel method, which sets the CancellationToken’s IsCancellationRequested property to true.

 

Output:

If you uncomment the Thread.Sleep and Cancel statements in method Main, the task is cancelled after three secondsand below is the output:

 

Waiting Asynchronously for Tasks in the async Method:

In your async method, if you want to wait on Tasks as your await expression. This allows your async method to return to the calling method, but allows the async method to wait for completion of one or all of a set of tasks. The calls that allow this are the Task.WhenAll and Task.WhenAny methods.

 

Output:

 

Summary:

In this article, we have discussed:

  • What is an async Method?
  • The Flow of Control in an Async Method
  • The await Expression
  • Exception Handling and the await Expression
  • Cancelling an async Operation
  • Waiting Asynchronously for Tasks in the async Method
  • Summary

You may also like:

Thanks for visiting !!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s