Asynchronous Task Composition | C# Advanced [6 of 8]


>>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 this really interesting. As people complain about how using
tasks going async is a virus, it’s like as soon as I brought
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. 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. Once that is done, then it will actually
return the data.>>So let’s go through some code and let’s see how that actually happen.>>Yeah, let’s see this an action.>>All right. So I’ve got
this application here, and it worries me because
it’s synchronous. So I start it, it’s going to
collect some data from some source, and it’s just sitting there.>>Yeah.>>I get worried some times that
this just isn’t even working. This is not good. I’m concerned. So it keeps going. Then finally, I get a whole bunch
of data, and it’s just all done.>>Yeah.>>All right. So let’s
make this asynchronous. So let’s look down into
what’s collecting data, and let’s have you help me do this.>>All right. Yeah.>>All right. So I’m reading a page, I’m going to stack
in some stuff lags.>>Yes. So instead of calling
ReadPage, call ReadPageAsync.>>All right. So this
is that first part, I’m not calling this
asynchronous method, then now I’m getting
some red squigglies.>>Yeah. So add the
await operator to that.>>Okay, because I don’t
like the red squigglies. They tell me I did something wrong.>>Then await is only
valid in an async method.>>Yeah.>>So we have to add the async
modifier to the method signature.>>I’m going to do this here.>>Yeah.>>I like the way Visual
Studio does this. It can just tell me it’s going
to make this method async.>>Well, that’s true.>>Because I’ve added
that. Now, it did more than just add async here. Now, what’s this other
stuff? So I’ve got this.>>Yeah, exactly. So now I change the return type
to a task of IEnumerable of int.>>All right.>>So does that all for you.>>So now that’s where you were
talking about this earlier, where if it’s async, instead of that IEnumerable of int, I’ve got returning a task of IEnumerable of int to carry
that extra information.>>Exactly, yes.>>Now, I’ve got no red squigglies
here, I’m really happy.>>Still have an error over there.>>I do. Let’s go click on
that and see what’s going on. So I got this foreach item in data, and I can’t foreach over stuff. So what do we have to do here?>>Yes. So you also have to
change that to an async.>>So I have to await this here?>>Yes, exactly.>>So I’m awaiting that. So
I’m awaiting other stuff, and now I have to
make this async too. Now it’s back up and it bubble all
the way up to my main program.>>Perfect.>>In this one, I’m done.>>Yeah. Is it easy?>>That’s cool. 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. Well, what’s going on?
So it still takes time. But at least now it’s
all asynchronous, and if this program were
in the 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 are telling me, this is 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 there just
changing the return type. It’s now asynchronous,
so I have to add those sprinkles
asynchronous words around.>>Exactly.>>So it doesn’t move
around my code quite a bit, but it’s not really that
bad. So it’s actually good.>>Yeah, it is.>>That was the tooling, it’s
pretty easy to make those changes.>>Well, though that
seems really simple. We just learned about making a
synchronous code asynchronous. In our next video, we’re going to talk about
handling asynchronous errors.

2 Replies to “Asynchronous Task Composition | C# Advanced [6 of 8]”

  1. AdamsTaiwan says:

    This really helps when I need a WPF UI to update while background processing.

  2. Dave Rooney says:

    Thanks for the video. One question though, why rename the method to have "Async" at the end? if this is out of the box then is there a way to turn that part off in visual studio? I don't like renaming a method when refactoring or creating async functions, I think making it return a Task is clear enough (unless I have an sync and an async version of the same function)

Leave a Comment

Your email address will not be published. Required fields are marked *