How do I understand asynchronous code?

Hello, Habr! I present to your attention the translation (with minor corrections) of the article "How Do I Think About Async Code ?!" by Leslie Richardson.



Asynchronous code is becoming more and more popular for writing responsive applications. Unfortunately, asynchronous programming also introduces additional difficulties. As a consequence, understanding how such code works can be a daunting task, regardless of your experience. If you are just getting started with asynchronous code, or want to brush up on your understanding, this is an introduction to the world of asynchronous programming!



What is Asynchronous Code?



Asynchronous programming allows you to execute a block of code without stopping (or blocking) the entire thread in which the action is being performed. A common myth about asynchronous code is that it improves performance, which is not always true. Instead, the main feature of asynchronous programming is that it increases the number of tasks (throughput) that can be executed concurrently, without having to block the thread in which these actions are performed.



You might think that asynchronous code is very similar to multi-threaded code, after all, many methods can be executed simultaneously in both cases. In fact, asynchronous programming can be used with both single-threaded and multi-threaded applications. This means that you can have a single-threaded asynchronous program in which one thread can run parallel tasks. Conversely, you can also have a multi-threaded asynchronous application where multiple threads can run multiple parallel tasks.



Why should I use asynchronous code? An example, please!



To have some analogy for demonstrating asynchronous programming, consider the process of baking a pie. This process will be represented by a thread that performs several steps (or tasks), as shown in the code below. This code is correct and you still have a delicious cake after the method is executed. However, since all the code is synchronous, each line will be executed sequentially. In other words, you will be standing completely still while waiting for the oven to finish preheating. But at the same time, you could make dough for your pie!



Synchronous MakeCake () Method



image



Synchronous cake baking program



image



In real life, you tend to divide this process into tasks, kneading the dough while the oven is heating up. Or make the icing while the cake is baked in the oven. This increases your productivity and allows you to bake the cake much faster. This is where asynchronous code comes in handy! By making our current code asynchronous, we can do other things to pass the time while we await the result of a task , such as baking a pie in the oven.

To do this, we will change our code, as well as add the PassTheTime method . Now our code saves the state of the task, starts another synchronous or asynchronous operation, and gets the result of the saved task when needed.



Asynchronous MakeCake ()

image



Asynchronous Cake Baking Program

image



Compared to the synchronous MakeCake method , which lacks the PassTheTime method , the asynchronous MakeCakeAsync method can complete more tasks without blocking the thread, which reduces the time it takes to complete the entire method.



Comparison of asynchronous and synchronous programs



image



How do I write asynchronous code in .NET?



C # allows you to write asynchronous code using the Task type and the await and async keywords . The Task type informs the caller of a possible return type. It also indicates that other actions can continue to be performed in the calling method. The async keyword is paired with the await keyword , which notifies the compiler that we need a return value from the method, but not immediately. As a result, we do not need to block the calling thread, and we can continue performing other tasks until the expected value is required. Initially asynchronous method will execute synchronously until await keyword is found... This is exactly the moment when the execution of the method will start asynchronously.



I learned about asynchronous code! Now what?



While an asynchronous cake baking application is great, there are many other real world applications that use asynchronous code as well. The two most common examples are:



Applications that use HTTP requests - Depending on the request, HTTP calls can take a long time to process. Using asynchronous code allows us to perform other operations while we wait for a response from the server.



Example HTTP GET Request



image



UI Applications - WPF applications or any other that uses buttons, text boxes, and other UX resources are also great for asynchronous implementation. For example, a WPF application that parses a file. This procedure may take some time. However, by making this action asynchronous, you can still interact with the user interface without completely stopping the application while waiting for the operation to complete.



All Articles