Asynchronous Programming in C# 5.0 - Access Data in Asynchronous Functions

Introduction 

 
Welcome to the Asynchronous Programming in C# 5.0 article series. In previous articles, we discussed many other topics in asynchronous programming. You can read them here.
  1. Asynchronous programming in C# 5.0: Part-1: Understand async and await
  2. Asynchronous Programming in C# 5.0 Part 2: Return Type of Asynchronous Method
  3. Asynchronous Programming in C# 5.0 Part 3: Understand Task in Asynchronous programming.
  4. Asynchronous Programming in C# 5.0 Part 4:Exception Handling in Asynchronous Programming
In this article, we will try to implement a few real-time applications of asynchronous programming. Here we will see in which scenario we can implement asynchronous threads. Let's try to understand the situation.
 
To call a Web service
 
If we want to call a web service, then we can implement an asynchronous function. This is because we know that in order to call a web service, it takes time (for other reasons, like low internet speed, server down and so on).
 
If we want our application's performance to not decrease due to the low response of the web service, then we can implement asynchronous functions, or we can call this web service asynchronously.
 
To pull huge amounts of data from a database
 
Let's think of one situation wherein one form, (Windows or Web Form for example), a huge amount of data needs to be pulled from the database. Now, if we access the data synchronously, then the entire UI will block and the user is unable to do anything until all database operations finish.
 
But if we do it asynchronously (or call the data fetching function asynchronously) then the user is free to do her work while the data is loading in the background.
 
Sometimes it's necessary to read a large file in an application. Reading the file using the StreamReader object takes time. We can read this file asynchronously, and then it will not affect the performance of the application.
 
I hope those three examples are enough to understand the real-time implementation of asynchronous functions. Now let's implement asynchronous functions in a few scenarios.
 
Fetch data asynchronously
 
We will create one sample program to fetch data asynchronously. We have created two functions to load data in the grid asynchronously. Let's create a simple user interface containing two grids and one button. When we press a button the grid will load asynchronously. Have a look at the following code: 
  1. using System;  
  2. using System.ComponentModel;  
  3. using System.Threading.Tasks;  
  4. using System.Windows.Forms;  
  5. using System.Data;  
  6. namespace WindowsFormsApplication1  
  7. {  
  8. public partial class Form1 : Form  
  9. {  
  10. DataTable dt = new DataTable();  
  11. public Form1()  
  12. {  
  13. InitializeComponent();  
  14. dt.Columns.Add("Id"typeof(int));  
  15. dt.Columns.Add("Name"typeof(String));  
  16. dt.Columns.Add("Surname"typeof(String));  
  17. dt.Rows.Add(1, "sourav""kayal");  
  18. dt.Rows.Add(2, "Ram""Kumar");  
  19. dt.Rows.Add(3, "Shyam""Kymar");  
  20. }  
  21. private void Form1_Load(object sender, EventArgs e)  
  22. {  
  23. }  
  24. public Task<DataTable> LoadData1()  
  25. {  
  26. return Task.Run(() => {  
  27. System.Threading.Thread.Sleep(10000);  
  28. return dt;  
  29. });  
  30. }  
  31. public Task<DataTable> LoadData2()  
  32. {  
  33. return Task.Run(() =>  
  34. {  
  35. System.Threading.Thread.Sleep(10000);  
  36. return dt;  
  37. });  
  38. }  
  39. private async void button1_Click(object sender, EventArgs e)  
  40. {  
  41. //Load Data Asynchronously  
  42. this.dataGridView1.DataSource = await LoadData1();  
  43. this.dataGridView2.DataSource = await LoadData2();  
  44. }  
  45. }  
  46. }  
Here is the sample output:

Async1.jpg

We can see that one grid has loaded and the data still is not available in the second grid. The most noticeable fact is that the user interface is not collapsed in the data fetch operation. We can drag this window anywhere and even resize it.

Async2.jpg
 
Now data has been populated in both grids.
 
So, this is an actual example of an asynchronous function. Here we have loaded static data using a data table, but in practical usage, you may implement data fetching operations from a database.
 

Conclusion

 
In this article, we learned how to access data asynchronously in C#. I hope you have understood the concept. In future articles, we will see a few more scenarios where we can implement the asynchronous mechanism.