Asynchronous I/O operations

When we were going through the WebRequest and WebResponse section, we wrote a program in which we made a call to google.com. In very crude terms, when the request is made, it's picked up by the Google server, which then assigns a thread to cater to this request. The thread then sends the response to the calling machine. 

Theoretically, however, there is always a possibility that fewer free threads are available on the server. Also, there is the possibility that the server may take a long time to complete the request and send the response to the caller. 

The challenge here is that we must design communication between the caller and the server is such a way that the performance and responsiveness of the application are not compromised. We do that by making the calls asynchronous. 

If we make these operations asynchronous, we can rest assured that, while the server is processing the request and sending us the response, our application remains responsive and users can continue using the application. We do this using the async/await keywords. 

Any method that is written asynchronously in C# must have the following characteristics:

  • The method definition must have the async keyword to indicate that the method is executed asynchronously. 
  • The method must have one of the following return types:
    • Task: If the function has no return statements
    • Task<TResult>: If the function has a return statement in which the object being returned is of type TResult
    • Void: If the function is an event handler
  • In the function, we execute an asynchronous call to an external web server.
  • The function may have an await statement. The await statement basically tells the compiler that the application must wait at that statement for the asynchronous process executed by the external web server to finish.

Let's go through all these points in the following code implementation:

async Task<int> ExecuteOperationAsync()
{
using (HttpClient client = new HttpClient())
{
Task<string> getStringTask = client.GetStringAsync("http://google.com");
ExecuteParallelWork();
string urlContents = await getStringTask;
return urlContents.Length;
}
}

Please refer to the following in the preceding code:

  • We have defined a function called ExecuteOperationAsync. To indicate the asynchronous behavior of the function, we have used the async keyword in the function definition. 
  • We have declared the return type of the function as Task<int>, which indicates that the function will return an object of type int.
  • We have declared an object of the HttpClient helper class and are making a call to http://google.com. We are making the request call asynchronously.
  • To ensure that the application carries on doing other work, we are calling the ExecuteParallelWork function so that, until the response arrives, the application does not stop processing. 
  • We have used an await statement so that the compiler stops at that point and waits for the response for a asynchronous request call. Once the response is received, it checks the length of the response string and returns the result to the calling function.

Now that we have a fair understanding of how asynchronous calls work in I/O operations, we will look at how to use them in I/O operations.

In the next section, we will look at how different I/O operations can be made asynchronous using this keyword.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.26.138