jay's old blog

this blog will be deleted soon - please visit my new blog - https://thesanguinetechtrainer.com

Async and Await Revisited - Update 1

I have already written about async, here. yet, thought I will just revisit here.

The thing is about async and await is all about improving responsiveness. What is responsiveness though? In different context, it takes on different meanings. In a website, the ability to move content around to suit the screen size is responsiveness. You see this a lot in mobile websites which also look equally good on desktop. In a application though, responsiveness comes down to the ability to keep the user engaged when stuff is happening in the background.

A simple everyday application activity is waiting for a photo to load. There would be a photo area, which will be surrounded by the usual application buttons and options. While the photo loads, the rest of the application should still be usable. Perhaps a loading screen with a percentage to show what is happening. Stuff like this is what async and await is all about. It is about making sure that the thread that manages the user interface is able to isolate itself from other stuff such as data queries and processing of data which is happening in the background.

This is the whole point of writing asynchronous code. keeping things from freezing or becoming unresponsive when stuff takes longer than expected.

To this effect, c sharp provides the async and await keywords. These keywords allow a particular method to be marked for the benefit of the compiler, indicating that stuff that follows will be asynchronous. Then, the compiler can go ahead create the necessary task related code. This is the key stuff about async and await. It takes the load off the developer from having to write actual asynchronous code, reducing errors in the process. This also means, all that stuff you know from reading about tasks and threads will come in handy here as well.

As always, find the code sample for this entry at my repo.

Follow me on twitter, facebook and instagram for more updates. Thanks!

Await and Async

Chances are you have already encountered this at some point or another. When you are working with tasks, you will eventually have to work with await and async.  

You need to understand these two words – Await and Async.  

async is a keyword that you use to indicate that a particular method has an 'await' call happening. Its really as simple as that. In technical terms, it means, that the code will be asynchronously. That brings us to the more important question, synchronous and asynchronous.  

Synchronous means, something that happens one after another. To take an everyday example, where you wanted to brush your teeth and then watch some television. Consider how you brush your teeth. You grab your toothbrush, apply some paste on it and finally brush your teeth. That is a 'one after another' sequence and it goes along that way. Then, you decide to go with that TV watching. That's synchronous. 

However, let's go with something else. Let's use the same example of brushing for teeth. You pick your toothbrush, and then go for the toothpaste. Then, you realize that you are out of paste. You ask your roommate to get the paste for you. Until the paste arrives, you cannot brush so you decide to watch some television which you were planning to do anyway. Then, when the toothpaste arrives, you stop the TV watching and resume the tooth brushing.  

That is how asynchronous works. Some things happen in sequence, and others don’t. Asynchronous programming allows you to program so that stuff that can happen synchronously happen that way, and those that need to be waited upon, will be awaited upon.  

Along with async, the await keyword is used to indicate that the code shall wait for the work to be completed. For instance, look at this. 

                string result = await client.GetStringAsync("http://www.studynildana.com"); 

The result string will get assigned what the 'GetStringAsync' task returns. You will notice that the task is connected to a web call. Depending on the internet connection speed, that could take a while. It could be less than a fraction of a second or a full 30 seconds. Yet, the task will return when it is finished. The results will get assigned with the returned value. When that happens, the method itself will return to the method that called it.  

On the main thread – which called this task- will do other stuff while waiting for this task. When task return with the string value, the main thread will do what it was asked to do when the method returns. Now, I must admit this whole asynchronous thing looks confusing. That is because, by design we have learnt to do things sequentially. However, software does not work that way. It does lots of things in the background while doing other stuff in the foreground. That is why, a modern application developer must know how to program asynchronously 

As usual, check our repo for actual code and see how it really works.