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:
- 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:
class Program
{
static void Main()
{
Task<int> value = DoAsyncWork.CalculateSumAsync(10, 11);
//Do Other processing
Console.WriteLine("Value: {0}", value.Result);
}
}
static class DoAsyncWork
{
public static async Task<int> CalculateSumAsync(int i1,int i2)
{
int sum = await Task.Run(() => GetSum(i1,i2));
return sum;
}
private static int GetSum(int i1, int i2)
{
return i1+i2;
}
}
Output:
Using an async method that returns a Task object:
class ProgramValue: 21
{
static void Main()
{
Task value = DoAsyncWork.CalculateSumAsync(10, 11);
//Do Other processing
value.Wait();
Console.WriteLine("Async stuff is done");
}
}
static class DoAsyncWork
{
public static async Task CalculateSumAsync(int i1,int i2)
{
int sum = await Task.Run(() => GetSum(i1,i2));
Console.WriteLine("Value: {0}", sum);
}
private static int GetSum(int i1, int i2)
{
return i1+i2;
}
}
Async stuff is done
Using an async method that returns a void object:
class ProgramOutput:
{
static void Main()
{
DoAsyncWork.CalculateSumAsync(10, 11);
//Do Other processing
Thread.Sleep(200);
Console.WriteLine("Program Exiting");
}
}
static class DoAsyncWork
{
public static async void CalculateSumAsync(int i1,int i2)
{
int sum = await Task.Run(() => GetSum(i1,i2));
Console.WriteLine("Value: {0}", sum);
}
private static int GetSum(int i1, int i2)
{
return i1+i2;
}
}
Value: 21
Program Exiting
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
No comments:
Post a Comment