From the course: C#: Advanced Practices
Asynchronous task composition - C# Tutorial
From the course: C#: Advanced Practices
Asynchronous task composition
- Hello again, in our last video, we taught you the basic syntax of async and await. In this one, what we're going to do is we're going to talk about how Task really helps make that work. So this is one of the questions we get that's really interesting. 'Cause people complain about how using Task and async is kind of like a virus, it's like, as soon as I put Task and await somewhere, now it's async here, Task there, await there, and pretty soon it's everywhere, it's a virus that ran through my entire code base. Is that true? - In some ways yes, but you can think of it like a method. So when you write a method, you expect it to return some data. And instead of that, it will actually return a Task and that Task is an object that will actually return the status of the asynchronous work, and once that is done, then it will actually return the data. - Okay, so let's go through some code, and let's see how that actually happens. - Yeah, let's see this in action. - All right, so I've got this application here, and it it worries me 'cause it's synchronous. So I start it, it's going to collect some data from some source and it's just sort of sitting there. And I get kind of worried sometimes that this just isn't even working, you know, this is not good, I'm kind of concerned. So it keeps going, and then finally I get a whole bunch of data and it's just all done. All right, so let's make this asynchronous. So let's look down into where it's collecting the data and let's have you help me do this. - [Mika] All right, yeah. - [Bill] All right, so I'm reading a page, and I'm going to start getting some stuff back. - [Mika] Yeah, so instead of calling read page, call read page async. - [Bill] All right, so this is that first part, I'm not calling this asynchronous method, and now I'm getting some red squiggles. - [Mika] Yeah, so add the await operator to that. - [Bill] Okay, 'cause I don't like the red squigglies. They tell me I did something wrong. - [Mika] And then a await is only valid in an async method, so we have to add the async modifier to the method signature. - [Bill] Okay, I'm going to do this here. I like the way visual studio does this, it can just tell me it's going to make this Method async, see now it's just added that. - [Mika] Well that too. - [Bill] Now it did more than just add async here. Now what's this other stuff, see now I've got the Task there. - [Mika] Yeah, exactly, so now I change the return type to a Task of I E numerable of int. So it did that all for you. - All right, okay so now that's where you were talking about this earlier, where if it's async, instead of that I E numerable event, I'm now returning a Task of I E numerable event. To carry that extra information. - Exactly, yes. - All right, now I've got no red squigglies here, I'm really happy about this. - You still have an arrow over there. - I do, I do, let's go click on that and see what's going on. So I've got this for each item and data, and I can't for each over stuff. So what do we have to do here? - [Mika] Yeah, so you'll also have to change that to an async. - [Bill] So I have to await this here. - [Mika] Yes, exactly. - [Bill] So I'm awaiting that. You know, I'm awaiting other stuff, and now I get this, I have to make this async too, all right say, and now it's back up and it'll bubble all the way up to my main program. - [Mika] Perfect. - [Bill] In this one I'm done. - [Mika] Yeah, easy peasy. - All right, that's cool, okay so let's run it. Now I know we didn't do anything to really introduce some of the things that would make it come back a lot faster, or do some other work while it's going on, so it still takes time, but at least now it's all asyncronus and if this program were to console app and we were doing other things, other things could be happening here. - Exactly. - So I like the way we did that. But if I understand what you were telling me, is, this is kind of like a refactoring, where all I've done is I've changed the return type, and then that has to bubble up to places where it's called and it isn't, you know, just changing the return type, it's now asynchronous, so I have to add those, sprinkle those async and await keywords around. - Yes, exactly. - Okay, so it does move around my code quite a bit, but it's not really that bad, it's actually kind of good. - [Mika] Yeah, it is. - [Bill] And with the tooling, it's pretty easy to make those changes. - Well bill, that seems really simple. We just learned about making synchronous code asynchronous, in our next video we're going to talk about handling asynchronous errors.
Contents
-
-
Introduction to Language Integrated Query (LINQ)5m
-
Learn LINQ query syntax5m 58s
-
LINQ query composition and execution4m 47s
-
LINQ, Entity Framework, and remote data sources8m 13s
-
Introduction to async, await, and tasks3m 45s
-
Asynchronous task composition3m 47s
-
Handling asynchronous exceptions4m 31s
-
Distinguish asynchronous and multithreading5m 36s
-