Asynchronous Programming3 min read

Have you ever been irritated by unresponsiveness of GUI? Clicking something and then waiting for GUI to get responsive again? If so, you have probably been a victim of synchronous programming. 🙂

So let’s scrape synchronous programming and say “Hi!” to asynchronous programming in C#.

Why use Asynchronous programing?

  • To implement better GUI usability.
  • To make our code easier to understand and write.

Aren’t there already ways to do asynchronous development? Well try using callbacks or threads. I guess you want be that much satisfied while writing your code, trying to figure it out and debugging.

When to use Asynchronous programing?

If you want to do any cost operations like accessing database or doing any kind I/O operations on files etc.

Luckily, .NET provides us with a bunch of async methods, which we can use easily inside our own methods.

How do we code?

Easy. By using these keywords:

  • async
  • await

and these methods which we combine with above keywords:

  • Task
  • Task<T>

So each method has to have async and Task/Task<T> as footprint and await inside body method. If you don’t have await it will simply pop you an error and your code won’t compile.

Show me the code!

public async Task<int> RetrieveNumberOfHoursAsync(){
   return await SomeExternalMethod.GetHoursAsync();
}

In above example, when we trigger our RetrieveNumberOfHoursAsync() method, our inbody function will fire up but won’t block the main thread. When all hours from SomeExternalMethod are retrieved, only then will our method trigger up the results back.

Let’s make it more clear:

public async Task<int> RetrieveNumberOfHoursAsync(){
    int hours = await SomeExternalMethod.GetHoursAsync(); // line 1
    Console.WriteLine("I won't execute until I get the results from SomeExternalMethod()"); // line 2
    return hours;
}

So what the above code does is, it triggers SomeExternalMethod() and stops from further executing.

So at first RetrieveNumberOfHoursAsync() call, only line 1 will execute. Then, only when SomeExternalMethod() is able to complete retrieving all hours, will line 2 be executed.

Let’s make it even more clear:

public async Task<int> RetrieveNumberOfHoursAsync(){
   Task<int> getHoursTask = SomeExternalMethod.GetHoursAsync(); // line 1
   Console.WriteLine("I will execute immediately"); // line 2
   int hours = await getHoursTask; // line 3
   Console.WriteLine("I won't execute until I get the results from SomeExternalMethod()"); // line 4
   return hours;
}

Triggering our method RetrieveNumberOfHoursAsync() we are going to create a Task<int> getHoursTask, (line 1) but we won’t execute it right away. Instead, we still want to do some additional stuff (line 2) before executing our newly created task (line 3). Only when our getHoursTask is finished with retrieving hours, will our message be written to console (line 4) and return number of hours.

Conclusion

Using async/await operators we make our code easier to read and maintain. It’s pretty easy to use and code with async/await. Anyways, there is a lot more to Task class than written here. Since you can use built in methods of Task class to do a bunch of manipulations with async methods. But I will leave this as homework for you. Go figure it out and make code cleaner and apps more user friendly!

All about Task class

In case you want to read little more about Task class itself, please visit it here: https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task?view=netframework-4.8

Leave a Reply

Your email address will not be published. Required fields are marked *