Asynchronous Programming

Few days before I have been assigned a new project with some awesome technologies so I started learning many new things and after couple of days I just said to myself I know everything (read I am great :)), so enough for learning and now start coding. Well this happiness doesn’t last long. After few days I was sitting in office using httpClient() class and saw ‘Async’ keyword on it’s methods. I just thought how ridiculous is Microsoft. Every time I think I know everything in C# it just come up with some new things(I am so frustrated ). So out of curiosity I have started looking into this Async by goggling and reading over MSDN and this way I started a new journey to learn a new concept. So as I think it’s worth share my findings. So here we go :

A Problem :

Imagine what the world would be like if people work the same way as computer programs:

void DevelopProject(Client client )
var specs = ObtainRequirement(client);
var arch = DecideArchAndTech(specs);
var developer = AssignDeveloper(specs);
var project = Developere.DevlopProject(arch);

Each task further can be broken in multiple task like Obtain Requirement might involve various discussion with client.Were humans to perform these sorts of tasks like typical computer programs, we’d carefully write down everything as sequences of hierarchical tasks in a checklist and obsessively ensure that each job was complete before embarking on the next.

This approach looks good because you can’t develop a project without getting the specs but if you do the same way it would be waste of time and make the application unresponsive because you want to assign some developer while architecture is busy to develop the architect or if some other customer came you want to start working over specs rather then waiting to complete the previous project

A Solution using Asynchronous Programming(Async/Await):

Let’s use the concept. I will explain the most of the things later so just hold on for a moment

async void DevelopProject(Client client )
var specs = await ObtainRequirementAsync(client);
var arch = await DecideArchAndTechAsync(specs);
var developer = await AssignDeveloperAsync(specs);
var project = await Developere.DevlopProjectAsync(arch);

Let’s review the New Keywords

async :
The “async” keyword enables the “await” keyword in that method. That’s all the async keyword does! It does not run this method on a thread pool thread, or do any other kind of magic. The async keyword only enables the await keyword.

The beginning of an async method is executed just like any other method. That is, it runs synchronously until it hits an “await” .

The “await” keyword is where things can get asynchronous. Await is like a unary operator: it takes a single argument, an awaitable (an “awaitable” is an asynchronous operation). Await examines that awaitable to see if it has already completed; if the awaitable has already completed, then the method just continues running (synchronously, just like a regular method).

If “await” sees that the awaitable has not completed, then it acts asynchronously. It tells the awaitable to run the remainder of the method when it completes, and then returns from the async method.

Later on, when the awaitable completes, it will execute the remainder of the async method. If you’re awaiting a built-in awaitable (such as a task), then the remainder of the async method will execute on a “context” that was captured before the “await” returned.

I like to think of “await” as an “asynchronous wait”. That is to say, the async method pauses until the awaitable is complete (so it waits), but the actual thread is not blocked (so it’s asynchronous).

Return Types
Async methods can return Task<T>, Task, or void. In almost all cases, you want to return Task<T> or Task, and return void only when you have to.

The CLR 4 release defined the type Task<T>—the workhorse type of the Task Parallel Library (TPL)—to represent the concept of “some work that’s going to produce a result of type T in the future.” The concept of “work that will complete in the future but returns no result” is represented by the non-generic Task type.

Precisely how the result of type T is going to be produced in the future is an implementation detail of a particular task; the work might be farmed out to another machine entirely, to another process on this machine, to another thread, or perhaps the work is simply to read a previously cached result that can be accessed cheaply from the current thread.

A Thing to care :

Async programming is introduced to make your work more responsive but overuse of these concept will work as overhead. So we need to decide carefully that where we are going to use these and does we really need to make the async call.

Reference :
I found the following link very useful to understand these concepts:
If you like this article and want to write me back to discuss over this further, you are welcome to leave comments or you can write me on my Email Id
Don’t forget to like and share..:)

Written By: Ashish Kumar, Software Engineer, Mindfire Solutions

Posted on June 20, 2014, in C# and tagged , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: