Asynchronous Programming in C# 5.0 Part Two: Return Type of Asynchronous Method

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:

Asynchronous programming in C# 5.0: Part-1: Understand async and await

In this article we will understand various return types of asynchronous functions. In our previous article we saw that there are three return types of any asynchronous function and 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 function. 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 event.

Let's see that in action.

using System;

using System.ComponentModel;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace WindowsFormsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

public static Task LongProcess()

{

return Task.Run(() =>

{

System.Threading.Thread.Sleep(5000);

});

}

public async void CallProcess()

{

await LongProcess();

this.listBox1.Items.Add("Long Process finish");

}

private void Form1_Load(object sender, EventArgs e)

{

}

private void button1_Click(object sender, EventArgs e)

{

this.label1.Text = "Return Void";

CallProcess();

this.listBox1.Items.Add("Program Finish");

}

}

}

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. And in the buttion's click event we are calling the CallProcess() function using the await keyword (in other words asynchronously). And 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 asynchronious) otherwise the complier will treat it as a normal synchronous function.

Have a look at the following code.
 

using System;

using System.ComponentModel;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace WindowsFormsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

public Task LongProcess()

{

return Task.Run(() => {

System.Threading.Thread.Sleep(5000);

});

}

public async Task CallProcess()

{

await LongProcess();

this.listBox1.Items.Add("Long Finish");

}

private void Form1_Load(object sender, EventArgs e)

{

}

private void button1_Click(object sender, EventArgs e)

{

this.label1.Text = "Return Task";

CallProcess();

this.listBox1.Items.Add("Program Finish");

}

}

}

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. Hope all of you understand the meaning of T. Cool! We will return a String from an asynchronous function. Look at the following code.
 

using System;

using System.ComponentModel;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace WindowsFormsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

public static Task<string> LongProcess()

{

return Task.Run(() =>

{

System.Threading.Thread.Sleep(5000);

return "Long Process Finish";

});

}

public async void CallProcess()

{

String Value = await LongProcess();

this.listBox1.Items.Add(Value);

}

private void Form1_Load(object sender, EventArgs e)

{

}

private async void button1_Click(object sender, EventArgs e)

{

this.label1.Text = "Return Task<T>";

CallProcess();

this.listBox1.Items.Add("Program Finish");

}

}

}

Asynchronous-programming-4.jpg

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

Conclusion

This article has explained three return types of asynchronous functions. Hope you have understood the concept. Comments are always welcome. In the next few articles we will dig more into the same topic. Have a nice day.
<< Previous article                                            Next article >>