Creating Await-Able Functions in .NET

Introduction

 
In my previous article, I talked about Asynchronous programming using the .NET framework. There we saw how to use the async and await keywords to make a function run in an asynchronous mode. We saw how to call various await-able functions provided by .NET. There are many functions provided by almost every major class of .NET which are await-able, and these mostly end with the letters Async to distinguish them from the regular functions. However, what happens when we want to change our own functions from being synchronous to be asynchronous. How can this be done? We will look at this in this article.

Changing your own functions to be await-able

 
We will be using a .NET framework Windows forms application to demonstrate the conversion of asynchronous function to an asynchronous function, or more simply put, we will create an asynchronous version of an existing function. So, let us begin.
 
We create a simple .NET framework Windows forms application and place one button (btnTest) and one label (lblValue) on it and add the code on the button click event as below:
  1. using System;  
  2. using System.Threading;  
  3. using System.Windows.Forms;  
  4.   
  5. namespace AsyncAwaitNF  
  6. {  
  7.     public partial class Form1 : Form  
  8.     {  
  9.         public Form1()  
  10.         {  
  11.             InitializeComponent();  
  12.         }  
  13.   
  14.         private void btnTest_Click(object sender, EventArgs e)  
  15.         {  
  16.             var a = GetValue();  
  17.             lblValue.Text = a;  
  18.         }  
  19.   
  20.         private string GetValue()  
  21.         {  
  22.             //Simulating a long running process  
  23.             Thread.Sleep(5000);  
  24.             return "Value is displayed now";  
  25.         }  
  26.   
  27.     }  
  28. }  
Here, we see that when we click the button, we call a function called “GetValue()”. This function does some long processing, which we have simulated using the Thread.Sleep function and after that, it returns a value that is displayed on a label in the main form.
 
This operation is synchronous, and the form is frozen while the function runs and cannot be used.
 
Next, we will create an asynchronous version of this function as shown below:
  1. using System;  
  2. using System.Threading;  
  3. using System.Threading.Tasks;  
  4. using System.Windows.Forms;  
  5.   
  6. namespace AsyncAwaitNF  
  7. {  
  8.     public partial class Form1 : Form  
  9.     {  
  10.         public Form1()  
  11.         {  
  12.             InitializeComponent();  
  13.         }  
  14.   
  15.         private async void btnTest_Click(object sender, EventArgs e)  
  16.         {  
  17.             var a = await GetValueAsync();  
  18.             lblValue.Text = a;  
  19.         }  
  20.   
  21.         private string GetValue()  
  22.         {  
  23.             //Simulating a long running process  
  24.             Thread.Sleep(5000);  
  25.             return "Value is displayed now";  
  26.         }  
  27.   
  28.         private async Task<string> GetValueAsync()  
  29.         {  
  30.             return await Task.Run(() =>  
  31.             {  
  32.                 //Simulating a long running process  
  33.                 Thread.Sleep(5000);  
  34.                 return "Value is displayed now";  
  35.             });  
  36.         }  
  37.   
  38.     }  
  39. }  
Here, you see that we name the function “GetValueAsync”. We could name it anything we like but we simply add the Async to the end of the function to indicate this is an asynchronous version of the function. Secondly, we return a Task of string rather than a simple string and call the function logic via the Task.Run function.
 
Now, if we run the application we see that when we click the button, the form remains responsive and the function is run in an asynchronous manner and after 5 seconds, we get the response from the function and display it on the main form in a label.
 
Creating Await-Able Functions In .NET
 

Summary

 
In this article, we have looked at how we can convert our regular synchronous function into an asynchronous one. The process is rather simple but before we do this, we must understand why we need to do this and only apply this logic to long-running functions which could freeze up the screen while the processing is in progress. If the function can be called and left to complete in the background, I would recommend using the Task Parallel Library to achieve this.


Recommended Ebook

Diving Into Microsoft .NET Entity Framework

Download Now!
Similar Articles