C# Asynchronous Programming - Return Type of Asynchronous Method

Before reading this article please go through the previous parts also.

Introduction 

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.

C# Asynchronous Programming

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.

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. 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.

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. 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.

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 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.

To gain better understanding of this article please read the next part also.


Recommended Free Ebook
Similar Articles