From the course: C#: Advanced Practices

Introduction to async, await, and tasks - C# Tutorial

From the course: C#: Advanced Practices

Introduction to async, await, and tasks

- Hi, my name is Mika. - And I'm Bill. - And our last series was on link and now we want to talk about async, await and task-based programming. So this topic generates a lot of questions amongst developers at all levels. Why is that Bill? - Well, it can be complicated, but I think you're well on your way after watching the videos in the introductory series and the ones on link. - Yeah. - Asynchronous programming can look hard because in order to do it, we often had to rearrange code so that it didn't read like what we wanted to happen. It would read like this jumble of moving from different places with continuations or promises in JavaScript or whatever, and it just didn't read right. Right so a developers want is to be able to have code that reads synchronously so they can understand what's going on at a quick glance. - Right and yet execute asynchronously. You know a great example is the way humans interact. Like if I were to describe how I would make a breakfast I would talk about something like, you know I want you to fry a couple eggs. Let's cook three slices of bacon. Let's have some coffee and some orange juice and let's make a couple pieces of toast with bread or with butter and jam. - Right, so I wouldn't want to do that, you know step by step, like first, let me find my bacon. And once that's done, let me find my eggs. Once that's done, let me-- - Right. - You know, make my toast. 'Cause then your breakfast is going to be cold and nobody wants a cold breakfast. - Exactly. - Am I right? But that's what computers do when they're doing synchronous code. So-- - Yeah, let's jump into some code and see this. - So if we have this as an example, very contrived example, we've got some code that makes breakfast. And this would execute synchronously. So it's taken those instructions that we have and just done them one after the other. So we've got the coffee, stop. Starting to cook the eggs and you can see these delays as we're waiting for things to happen. And now my eggs are getting cold already. - [Mika] Yeah. - [Bill] And now we're finally starting the toast. And the bacon's getting cold. - Now the bacon's ready. Yeah. - Yeah. And there we go. We don't want that, but we want it to read like this. - [Mika] Yeah, so do you have another example of something that's asynchronous? - We do. Let's take something that is basically the same program but now we've added the things that makes it async by using the keywords in C#. - Very nice. Yeah, so these both look very similar, right? They're just a set of steps. The left side, you have the synchronous example and then the right side, it looks pretty similar. That's the async example except I do notice those keywords await and task. - [Bill] Right, so this now returns a task 'cause it's asynchronous. And you see that async keyword which says this code should be async and the compiler has to rearrange it a little bit. And then down below, after we've started the tasks, well we've got that await that says, well I can't actually start eating until everything's done. So when all those tasks are done, we can continue. - [Mika] Great and you mentioned that it'll compile a bit differently. Can we see an example of that? - Well, let's go ahead and run this. And now you can see it's started to put some stuff in the toaster. It's already working on the bacon. The egg pan is going. because this is just in a console app, it doesn't necessarily work exactly like we'd want in real life, but it's a pretty reasonable simulation. It started all those tasks and it kept running and then it's ready a little faster and everything's already close to the same time. - All right, sweet. And where can everyone learn more about this? - Well, this example that we just went through is from one of our docs right here, an Introduction to Asynchronous Programming With Async and Await" just like we started talking about. The link is in the description for this video. And this takes you through and walks you through all the steps from that synchronous version we started with and ends up even making a few enhancements after the version we just showed you. So it's about a 10 minute read and it is a great introduction to asynchronous programming with async and await. - That's great. So our next video we're going to talk about tasks and tasks compose.

Contents