Your Ultimate async / await Tutorial in C#


Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Let's take a look at this example of a CPU-bound asynchronous method.

Click run to see the console output
ยทusing System;
using System.Threading.Tasks;
namespace AsyncApp
class Program
static void Main(string[] args)
var totalAfterTax = CalculateTotalAfterTaxAsync(70);
private static void DoSomethingSynchronous()
Console.WriteLine("Doing some synchronous work");
static async Task<float> CalculateTotalAfterTaxAsync(float value)
Console.WriteLine("Started CPU Bound asynchronous task on a background thread");
var result = await Task.Run(() => value * 1.2f);
Console.WriteLine($"Finished Task. Total of ${value} after tax of 20% is ${result} ");
return result;

In the above program, we have added an async method, CalculateTotalAfterTaxAsync, that takes an argument of float which represents the value to calculate upon.

Inside the method, if you notice, we are awaiting a task created on a background thread. As mentioned previously, we use this methodology, CPU Bound asynchronous method, when we need to do some offline heavy calculation that doesn't rely on specific I/O operation.

Once the task is completed. it will resume the CalculateTotalAfterTaxAsync with the statements after the await line. So it will displaying the Total after calculating the tax.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content