filmov
tv
What are ASYNC and AWAIT in C#? Asynchronous Programming Tutorial
Показать описание
What are ASYNC and AWAIT in C#? Asynchronous Programming Tutorial
The world of asynchronous programming can be daunting at first. Using async and await in C# is a proven way to write code that does not block the main thread, which means it will scale better and be responsive even when dealing with long-running tasks such as IO operations. This video will cover what ASYNC and AWAIT are, how they work, and best practices for using them together.
Asynchronous programming
Asynchronous programming is a means of parallel programming in which a unit of work runs separately from the main application
Normally, any code runs straight along, with only one thing happening at once(line by line). If a method calls another method, it has to wait for the other method to finish and return, and until that happens, the entire program is essentially stopped from the user's perspective. Even the UI would freeze and look unresponsive until the task is done.
This is where Asynchronous programming comes in handy.
Asynchronous programming is a means of parallel programming in which a unit of work runs separately from the main application.
Now when should we make our apps run tasks in parallel?
Let’s look at the following real-world scenario from the MS Doc.
Imagine we are cooking breakfast.
We want to make coffee, eggs, and bacon with some toast and jam, and some juice to wash it all down : ).
If we wait for each task to be done to move on to the next task, it will take around 30 minutes. But if you think about it, we don’t need to wait for the eggs to be done to fry the bacon!.
This is an asynchronously prepared breakfast. We can actually speed up this process and optimize it using the Asynchronous method.
Now using the Asynchronous method.
We can first pour the coffee, then start with frying the eggs and the bacon and turning on the toaster at the same time!.
Once the toast is made, we can put some jam on it, pour the juice, and the breakfast is ready!.
So tasks that do not depend on other tasks like frying the eggs and the bacon can be started together. Tasks that depend on other tasks like putting jam on toast must wait for the toasting step to be done. Of course, don’t forget to take off the eggs and bacon, so you don’t burn them ;).
This whole process will take less than 20 minutes, thanks to the Asynchronous method of preparing breakfast.
So how can we apply this approach to our programs ?.
.
tutorialsEU offers you free video tutorials about programming and development for complete beginners up to experienced programmers.
This includes C#, Unity, Python, Android, Kotlin, Machine Learning, etc.
Комментарии