Await and async

I was working on an project that was using the async and await keywords and an college asked what/why use the await/ async keywords when you will still be waiting on the task / thread to finish. Which was an very good question, and the best answer that I could give (of which I am writing it here) is that if you want to start an thread and let it process it’s work whilst still carrying on with the main thread then you would need to write some thread controls etc. But .Net gives you that thread control with the async / await keywords.

The async is short for asynchronous which basically means do something whilst doing something else, e.g. you are able to talk and listen at the same time!.

So what happens is that you can create an method to do some long processing, I am just using an Task delay here to simulate that with also using the await syntax just before it so that it will actually wait for the task delay to finish, as below

 public async Task<Boolean> getCheckAsync(Boolean c)
        {
            System.Console.WriteLine("getCheckAsync about to wait");
            await Task.Delay(1000);
            System.Console.WriteLine("getCheckAsync finished waiting");
            // do some processing.. pulling data from a database / website etc.
            if (c)
                return false;
            else
                return true;
        }

You have to decorate the method call with async Task, since then you will be able to do some await on this method.

The next part is to create an call to this method, and this will start the call as well — start processing the method of getCheckAsync

 Task<Boolean> getCheckAsyncTask = this.getCheckAsync(t);            // calls the process to start processing -- e.g. will output "getCheckAsync about to wait"

And to await for the result, which means that you are able to do some other actions in between the calling the method (getCheckAsync) and waiting on the result.

Boolean res = await getCheckAsyncTask;

So from the full code example below, the first output would be

Calling the async
Waiting for result from getCheckAsync
getCheckAsync about to wait
Bit inbetween the getCheckAsync call

which as you can see has already called the getCheckAsync method and then carried on processing the bit inbetween that is not reliant on the result of the method call (getCheckAsync), but once we are ready for the result of the getCheckAsync method call, the output would carry on with

getCheckAsync finished waiting
Obtainted the result from getCheckAsync
Passed the 'Calling the async'
B = False

Here is the code in full

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace asynctest
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
 
            System.Console.WriteLine("Calling the async ");
 
            Boolean b = p.getCheck(true).Result; 
 
            System.Console.WriteLine("Passed the 'Calling the async'");
            System.Console.WriteLine("B = "+b);
            System.Console.ReadLine();
        }
 
        public async Task<Boolean> getCheck(Boolean t)
        {
            System.Console.WriteLine("Waiting for result from getCheckAsync ");
            Task<Boolean> getCheckAsyncTask = this.getCheckAsync(t);            // calls the process to start processing -- e.g. will output "getCheckAsync about to wait"
            System.Console.WriteLine("Bit inbetween the getCheckAsync call");   // do some extra processing.
            Boolean res = await getCheckAsyncTask;                              // but now we have to "await" for the result.
            System.Console.WriteLine("Obtainted the result from getCheckAsync ");
            return res;
        }
 
        public async Task<Boolean> getCheckAsync(Boolean c)
        {
            System.Console.WriteLine("getCheckAsync about to wait");
            await Task.Delay(1000);
            System.Console.WriteLine("getCheckAsync finished waiting");
            // do some processing.. pulling data from a database / website etc.
            if (c)
                return false;
            else
                return true;
        }
 
    }
}

Leave a Reply

Your email address will not be published.