User Name:

User Email:

This information will only be saved for the purposes of communicating with those who have provided this information voluntarily regarding our services.We will never sell your name or email address to anyone.
© 2021 - First Crazy Developer (Abhishek Kumar)

crazydeveloper Async Feature in C# 5.0

async & await two keywords introduce in C# 5.0. Which allowed you to write asynchronous  Programming in easily and proper way. Before this writing an asynchronous  Programming is complicated with callback function.

We need to use combination of both keywords to write an asynchronous code. We can use await operator  with more than one expression in async method. We can say that both keywords in C# are the heart of async programming. A method whis is defined by using with these keywords as reffered and you can say identified  to as async methods.

Following areas where asynchronous programming help us

Area Supporting APIs contain async methods
Web access

HttpClient , SyndicationClient

Working with files

StorageFile, StreamWriter, StreamReader, XmlReader

Working with images

MediaCapture, BitmapEncoder, BitmapDecoder

WCF programming Synchronous and Asynchronous Operations

//Sample Syntax

public async Task<IEnumerable<File>> GetFileList()
 HttpClient fileClient = new HttpClient();
 Uri fileAddress = new Uri("");
 fileClient.BaseAddress = fileAddress;
 HttpResponseMessage getFileListresponse = await fileClient.GetAsync("fileService/File/FileList");
 if (getFileListresponse.IsSuccessStatusCode)
 var fileList = await getFileListresponse.Content.ReadAsAsync<IEnumerable<File>>();
 return fileList;
 return null;

Note for Async Feature

  1. The method signature includes an Async or async modifier.
  2. The name of an async method, by convention, ends with an "Async" suffix.
  3. The method usually includes at least one await expression, which marks a point where the method can't continue until the awaited asynchronous operation is complete. In the meantime, the method is suspended, and control returns to the method's caller. The next section of this topic illustrates what happens at the suspension point.
  4. The return type is one of the following types:

  • Task if your method has a return statement in which the operand has type TResult.
  • Task if your method has no return statement or has a return statement with no operand.

crazydeveloper Home Page 06 May 2015

Become a Fan