C# Asynchronous Programming - Return Type of Asynchronous Method

Introduction 

 
Welcome to the Asynchronous Programming in C# 5.0 article series. The previous article explained what the async and await keywords are. You can read it here:
 
 
In this article, we will understand the various return types of asynchronous functions. In our previous article, we saw that there are three return types of any asynchronous function. They are:
  • Void
  • Task
  • Task<T>
Oh, you don't see any difference between Task and Task<T>, right? Then that means there are two return types of asynchronous functions. Let's discuss each of them one by one.
 
Return void from asynchronous method
 
Though it's not recommended to return void from an asynchronous function, we can return void theoretically. Now, the question is, why is it not recommended to return void? The answer is if we return void then the caller function will not be informed of the completion of the asynchronous function. OK, then in which scenario can we return void? When we want to call an asynchronous function from an event (like a button click) then we can specify void in the event.
 
Let's see that in action:
  1. using System;  
  2. using System.ComponentModel;  
  3. using System.Threading.Tasks;  
  4. using System.Windows.Forms;   
  5.   
  6. namespace WindowsFormsApplication1  
  7. {  
  8. public partial class Form1 : Form  
  9. {  
  10. public Form1()  
  11. {  
  12. InitializeComponent();  
  13. }  
  14.   
  15. public static Task LongProcess()  
  16. {  
  17. return Task.Run(() =>  
  18. {  
  19. System.Threading.Thread.Sleep(5000);  
  20. });  
  21. }  
  22.   
  23. public async void CallProcess()  
  24. {  
  25. await LongProcess();  
  26. this.listBox1.Items.Add("Long Process finish");  
  27. }  
  28.   
  29. private void Form1_Load(object sender, EventArgs e)  
  30. {  
  31. }  
  32.   
  33. private void button1_Click(object sender, EventArgs e)  
  34. {  
  35. this.label1.Text = "Return Void";  
  36. CallProcess();  
  37. this.listBox1.Items.Add("Program Finish");  
  38. }  
  39. }  
  40. }  

Asynchronous-programming-1.jpg

 
Now, one condition is when we want to return void. We cannot use the await keyword when we want to return void from an asynchronous function.



Asynchronous-programming-2.jpg

 
In the above example, CallProcess() is an asynchronous function and it's returning void. In the button's click event, we are calling the CallProcess() function using the await keyword (in other words asynchronously). The compiler complains.
 
Return Task from asynchronous method
 
Let's see how to return a task from an asynchronous method. Basically the returning task is nothing but sending one signal to the caller function that the task has finished. When a method returns a task, we can use the await keyword to call it.
 
Note: The await keyword should be within a function qualified by an async keyword (in other words asynchronous), otherwise the compiler will treat it as a normal synchronous function.
 
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.   
  6. namespace WindowsFormsApplication1    
  7. {    
  8. public partial class Form1 : Form    
  9. {    
  10. public Form1()    
  11. {    
  12. InitializeComponent();    
  13. }    
  14. public Task LongProcess()    
  15. {    
  16. return Task.Run(() => {    
  17. System.Threading.Thread.Sleep(5000);    
  18. });    
  19. }    
  20. public async Task CallProcess()    
  21. {    
  22. await LongProcess();    
  23. this.listBox1.Items.Add("Long Finish");    
  24. }    
  25. private void Form1_Load(object sender, EventArgs e)    
  26. {    
  27. }    
  28. private void button1_Click(object sender, EventArgs e)    
  29. {    
  30. this.label1.Text = "Return Task";    
  31. CallProcess();    
  32. this.listBox1.Items.Add("Program Finish");    
  33. }    
  34. }    
  35. }  
Asynchronous-programming-3.jpg
 
Here we are returning a task from the LongProcess() function so that we can call it using the await keyword.
 
Return Task<T> from asynchronous method
 
Now, let's see how to return Task<T> from an asynchronous method. I hope all of you understand the meaning of T. Cool! We will return a String from an asynchronous function. Look at the following code:
  1. using System;    
  2. using System.ComponentModel;    
  3. using System.Threading.Tasks;    
  4. using System.Windows.Forms;     
  5.   
  6. namespace WindowsFormsApplication1    
  7. {    
  8. public partial class Form1 : Form    
  9. {    
  10. public Form1()    
  11. {    
  12. InitializeComponent();    
  13. }    
  14. public static Task<string> LongProcess()    
  15. {    
  16. return Task.Run(() =>    
  17. {    
  18. System.Threading.Thread.Sleep(5000);    
  19. return "Long Process Finish";    
  20. });    
  21. }    
  22. public async void CallProcess()    
  23. {  
  24. String Value = await LongProcess();    
  25. this.listBox1.Items.Add(Value);    
  26. }    
  27. private void Form1_Load(object sender, EventArgs e)    
  28. {    
  29. }    
  30. private async void button1_Click(object sender, EventArgs e)    
  31. {    
  32. this.label1.Text = "Return Task<T>";    
  33. CallProcess();    
  34. this.listBox1.Items.Add("Program Finish");    
  35. }    
  36. }    
  37. }  

Asynchronous-programming-4.jpg

 
From the LongProcess() we are returning a string to the CallProcess() function.
 

Conclusion

 
This article has explained three return types of asynchronous functions. I hope you understood the concept. Comments are always welcome. In the next few articles, we will dig more into the same topic. Have a nice day.
 

 Next article >> Part Three: Understand Task in Asynchronous Programming