Friday, July 31, 2020

C# Anatomy of Async and Await - Part 2

This is second part of C# Anatomy of Async and Await series, feel free to go through all parts listed following :

Today we are going to see some of the best practices to get maximum out of Asyn & Await in C#. First it is not recommended at all to call asynchronous methods synchronously but lets say in some situation we have to some how, then we should make sure that we are not calling  

Synchronization context is another key factor which we usually ignore, it means that to return execution control to the same thread which started that asynchronous task, it is important when the stater thread is UI but it make no sense if it was not main / UI thread, in that case the cost of context switching will be apply and overall performance of the application will hurt.
So the rule is if its a web app / api apply ConfigureAwait(false) for every asynchronous call, only avoid it if we want to return execution control to starter thread ( UI tread , caller ).

Most of the time a code base is structured in layered form, where we have separate modules in separate projects and all those library projects are referenced from within the main or executing project. You will agree with me that in most of the cases one library method get called from within another and this chain keep growing till last library get called from the executing project where the actual results are required. So the reason to create that scenario is that when this happen make sure you return Task from the methods if the output is not required for further processing. In short if we have method which can return task, don't make it async, in this way code will run little more fast as we will not be doing context switching.

Next is never wrap method call returning a task into try catch block, because that piece of code will run synchronously and if any exception get occurred in that task will not be pop up because the execution control is not waiting for that task to get completed.

Those were some of the most important points I wanted to share in this blog post, hope that will help you to improve your code and its performance and yes there is a lot more to come ;)

Sunday, July 5, 2020

C# Anatomy of Async and Await - Part 1

This is first the part of C# Anatomy of Async and Await series, feel free to go through all parts listed below :

I have been using async and await keywords for a long time and I feel that these should be explained in light of my experience. There are many cases for using them but generally, we need them when we don't want our program to get halt while making some API call ( http / web request ). Some other cases for using them are :

  • CPU bound task that took time ( using Task.Run )
  • Accessing file resources ( async streams )

Above was a little context that will help us to understand the internal working of an async task. Let’s jump right into points that will assist us to understand the anatomy and describe some brief explanations so that we can understand it clearly. Thus the first point is : 

# First, we use async with a method which has await keyword inside its body. We can't use one without the other, it’s the rule.

Explanation : There will be compile time error if we miss any keyword.

#Second, we only use await keyword with method call ( await SomeMethod(); ) which returns Task or Task<T>

Explanation : When we call a method which returns Task or Task<T>, the current thread starts executing that statement as normal fashion and if there are some more statements in the program that are needed to be executed, then the main control passes the execution of that method to another thread ( from thread pool ) and jumps back to the next statement and the only way to get results out from that method is to put await keyword in front of it.

And this is enough for this part, now we can see the real benifit of async and await keywords in context of tasks and if we conclude our findings we can say, multiple methods that are returning task ( or consuming tasks ) can be run sequentially if we put await in front of their calls.
Or can run concurrently on a separate Thread if we just call them and await them at the end of the calling function which will save our time for executing those functions.

I hope above article will help you to understand the anatomy of Async and Await keywords, and will give you at least a picture that how they work, there are a lot of parts in queue, meanwhile if you have any point, feel free to add comment, and stay tuned.

.NET 7 Minimal APIs Series - An Introduction

Introduction to .NET 7 Minimal APIs The release of .NET 6 brought about several exciting features and improvements, and one of the most exci...