jay's old blog

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

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. 

Comments are closed