WEBVTT

00:00:00.000 --> 00:00:02.910
>> Hey, friends. Were back
with my good friend Dean

00:00:02.910 --> 00:00:05.760
talking in advanced
Async Await, so tune in.

00:00:05.760 --> 00:00:12.810
[MUSIC]

00:00:12.810 --> 00:00:14.925
>> Welcome back everyone
to the Xamarin Show.

00:00:14.925 --> 00:00:16.110
I'm your host James Montemagno.

00:00:16.110 --> 00:00:18.570
Today, back for the second
time my good friend

00:00:18.570 --> 00:00:21.540
Dean on talking about Async
Await. How's it going, Dean?

00:00:21.540 --> 00:00:22.380
>> Good. How are you?

00:00:22.380 --> 00:00:24.030
>> I'm doing absolutely fantastic.

00:00:24.030 --> 00:00:26.100
Now, in the first episode,

00:00:26.100 --> 00:00:29.610
you showed off basic use
cases of Async Await,

00:00:29.610 --> 00:00:31.665
what's wrong, what's good.

00:00:31.665 --> 00:00:33.150
What you got in store for us today?

00:00:33.150 --> 00:00:34.200
>> So this time around,

00:00:34.200 --> 00:00:35.700
we're going for advanced scenarios.

00:00:35.700 --> 00:00:37.290
A bunch of edge cases.

00:00:37.290 --> 00:00:39.780
Advanced scenarios using
different mechanisms

00:00:39.780 --> 00:00:41.610
within the TPL and going deep.

00:00:41.610 --> 00:00:43.440
>> Very awesome. Cool. I love it.

00:00:43.440 --> 00:00:45.840
Well, let's waste no time at all
and let's get into the codes.

00:00:45.840 --> 00:00:47.565
>> Let's get right to it. All right.

00:00:47.565 --> 00:00:50.295
For the last one this is the most,

00:00:50.295 --> 00:00:51.750
I don't want to say complicated,

00:00:51.750 --> 00:00:53.640
but a very big edge case.

00:00:53.640 --> 00:00:54.165
>> All right.

00:00:54.165 --> 00:00:57.225
>> So what this is long running
tasks. So you're familiar?

00:00:57.225 --> 00:00:58.035
>> I am.

00:00:58.035 --> 00:01:00.260
>> So long running tasks in

00:01:00.260 --> 00:01:02.960
the TPL are tasks that
can run for a long time.

00:01:02.960 --> 00:01:04.850
Sometimes, you just
want something to run.

00:01:04.850 --> 00:01:09.470
Maybe it's pulling, maybe it's
checking your accelerometer values,

00:01:09.470 --> 00:01:10.880
something like that,
running in the background.

00:01:10.880 --> 00:01:12.745
You don't really want to stop it.

00:01:12.745 --> 00:01:15.360
So in a bad case,

00:01:15.360 --> 00:01:17.220
in my bad example here,

00:01:17.220 --> 00:01:19.190
I'm going to start

00:01:19.190 --> 00:01:23.285
a 100 tasks that are
printing dots every second.

00:01:23.285 --> 00:01:24.350
>> That makes sense.

00:01:24.350 --> 00:01:25.805
>> They're just going to be running.

00:01:25.805 --> 00:01:28.410
Fire and forget, forget about
them, and they'll just run.

00:01:28.410 --> 00:01:30.210
>> Visual Studio is even yelling you.

00:01:30.210 --> 00:01:33.060
>> Exactly. The Visual
Studio is smarter than me.

00:01:33.060 --> 00:01:36.020
Then, on top of that,

00:01:36.020 --> 00:01:38.500
I'm going to run a 100 tasks
that actually complete.

00:01:38.500 --> 00:01:39.240
>> Okay.

00:01:39.240 --> 00:01:40.550
>> So we'll see what
happens to these.

00:01:40.550 --> 00:01:43.310
So these tasks are the
tasks that in your app,

00:01:43.310 --> 00:01:44.660
you actually want to run,

00:01:44.660 --> 00:01:46.400
maybe you're downloading HTML,

00:01:46.400 --> 00:01:48.920
downloading images,
doing things like that.

00:01:48.920 --> 00:01:50.810
All right. So here's a bad example.

00:01:50.810 --> 00:01:54.920
We're running a 100 tasks
which print dots every second.

00:01:54.920 --> 00:01:57.575
So each one of these
tasks is printing a dot.

00:01:57.575 --> 00:01:59.705
Every single second,
it's firing forget.

00:01:59.705 --> 00:02:02.374
Then, we're starting
a 100 other tasks

00:02:02.374 --> 00:02:04.975
which are the tests that
we actually want to run.

00:02:04.975 --> 00:02:07.160
>> I see. So what are we seeing
in the output here then?

00:02:07.160 --> 00:02:08.240
>> So in the output here,

00:02:08.240 --> 00:02:10.370
we'll see that a bunch

00:02:10.370 --> 00:02:12.350
of threads are starting
from the thread pool.

00:02:12.350 --> 00:02:15.545
This is.NET managing
our threads for us.

00:02:15.545 --> 00:02:16.040
>> Okay. Got it.

00:02:16.040 --> 00:02:20.285
>> We talked about before. So as
you can see, it's really slow.

00:02:20.285 --> 00:02:23.090
There's no weight in
between these two.

00:02:23.090 --> 00:02:26.725
So the second batch of a 100
tasks should already be running.

00:02:26.725 --> 00:02:29.940
But, because these are so insistent,

00:02:29.940 --> 00:02:32.630
they're running every
second for 100 tasks.

00:02:32.630 --> 00:02:35.120
It's really taking
a while to spin up.

00:02:35.120 --> 00:02:38.150
So we'll see once the thread
pool catches up here.

00:02:38.150 --> 00:02:41.380
We should start seeing these
tests firing off. There we go.

00:02:41.380 --> 00:02:42.130
>> There we go.

00:02:42.130 --> 00:02:43.340
>> So these are the tasks that we

00:02:43.340 --> 00:02:45.275
want and they're
running to completion.

00:02:45.275 --> 00:02:46.610
But, in-between, you'll see that

00:02:46.610 --> 00:02:50.205
the previous task fire and forget
are still printing their dots.

00:02:50.205 --> 00:02:53.280
>> I see. Got you. So
everything is all.

00:02:53.280 --> 00:02:56.655
Also, we see this garbage collection
going crazy a little bit.

00:02:56.655 --> 00:02:57.840
It's not very happy with us, yeah.

00:02:57.840 --> 00:03:02.300
>> No. This this isn't too
uncommon to be honest.

00:03:02.300 --> 00:03:05.090
We've seen cases in this
where the thread pool is

00:03:05.090 --> 00:03:08.225
just suffocated and no tasks
can start for minutes.

00:03:08.225 --> 00:03:10.970
Sometimes, if you use it incorrectly,

00:03:10.970 --> 00:03:12.320
it can get jammed up.

00:03:12.320 --> 00:03:14.045
>> Got you. So how do I fix this?

00:03:14.045 --> 00:03:16.070
>> So let's just restart

00:03:16.070 --> 00:03:18.215
the app to make sure that
those tasks are dead.

00:03:18.215 --> 00:03:19.550
>> Got you.

00:03:19.550 --> 00:03:21.545
>> Then, we'll go to the fix.

00:03:21.545 --> 00:03:21.995
>> Sure.

00:03:21.995 --> 00:03:25.340
>> So we do have the
tools to do this.

00:03:25.340 --> 00:03:27.965
We can have a bunch of long
running in the background,

00:03:27.965 --> 00:03:31.610
but what we need to do is
use Task.Factory.StartNew.

00:03:31.610 --> 00:03:34.055
This code inside is the same.

00:03:34.055 --> 00:03:35.960
We're just printing out those dots.

00:03:35.960 --> 00:03:39.420
But then, in the parameter
for creation options,

00:03:39.420 --> 00:03:42.135
you need to add
TaskCreationOptions.Longrunning.

00:03:42.135 --> 00:03:45.945
>> I see. So this is something
built-in to.NET task factory.

00:03:45.945 --> 00:03:48.570
What's the difference
between this Task.Start

00:03:48.570 --> 00:03:50.460
and task factory?
Start here, I guess.

00:03:50.460 --> 00:03:52.110
>> So Tasks.Run is actually

00:03:52.110 --> 00:03:56.430
Task.Factory.StartNew with
the default parameters.

00:03:56.430 --> 00:03:57.915
It's a shortcut for us.

00:03:57.915 --> 00:03:58.440
>> Got you.

00:03:58.440 --> 00:04:01.100
>> So Task.Factory.StartNew
is more fine-grained.

00:04:01.100 --> 00:04:02.180
We have a bunch of parameters.

00:04:02.180 --> 00:04:03.695
I added the link to the reference.

00:04:03.695 --> 00:04:04.955
>> Beautiful.

00:04:04.955 --> 00:04:08.180
>> There's a lot more
than we can fit in there.

00:04:08.180 --> 00:04:11.785
But, basically, when we set
a task as long running,

00:04:11.785 --> 00:04:15.695
it has a separate thread
pool that it's smart about

00:04:15.695 --> 00:04:19.655
and it'll be able to oversubscribe.

00:04:19.655 --> 00:04:23.510
You can read the documentation.
So let's take a look.

00:04:23.510 --> 00:04:26.500
This is the only change,
Task.Factory.StartNew.

00:04:26.500 --> 00:04:30.540
So we'll see, these are the
100 tasks that print dots.

00:04:30.540 --> 00:04:33.420
Our other tasks are already starting.

00:04:33.420 --> 00:04:34.185
>> Yeah.

00:04:34.185 --> 00:04:36.465
>> Just like that. We
didn't even have to wait.

00:04:36.465 --> 00:04:38.580
>> So previously, we
actually had a wafer.

00:04:38.580 --> 00:04:41.100
A bunch of them, all they get
scheduled. Everything happened.

00:04:41.100 --> 00:04:44.945
So now, we're actually seeing
things work as expected.

00:04:44.945 --> 00:04:46.985
From the very beginning,

00:04:46.985 --> 00:04:49.590
all the tasks were all
running concurrently.

00:04:49.590 --> 00:04:50.220
>> Exactly.

00:04:50.220 --> 00:04:52.025
>> The long running is
still continuing on.

00:04:52.025 --> 00:04:55.310
You've already completed those
100 tasks where before we sat

00:04:55.310 --> 00:04:58.565
for what like 20 seconds
waiting for it. That's crazy.

00:04:58.565 --> 00:04:58.790
>> Yeah.

00:04:58.790 --> 00:04:59.480
>> Very cool.

00:04:59.480 --> 00:05:00.305
>> It's insane.

00:05:00.305 --> 00:05:02.115
>> Well, Dean, this is awesome.

00:05:02.115 --> 00:05:04.800
Just so much great content
just packed in right there.

00:05:04.800 --> 00:05:05.160
>> Thanks, James.

00:05:05.160 --> 00:05:07.550
>> Now, of course, we'll put
all the links to the show in

00:05:07.550 --> 00:05:10.490
the show notes below for
all of the best practices,

00:05:10.490 --> 00:05:12.545
documentation, this
sample code, everything.

00:05:12.545 --> 00:05:17.030
You can also go to
aka.MS/xamarinbestpractices

00:05:17.030 --> 00:05:19.310
to find out more about all
of the best practices in

00:05:19.310 --> 00:05:21.820
Xamarin for the mobile CAT
Team which is super awesome.

00:05:21.820 --> 00:05:23.695
Dean, thank you so much
for showing this all off.

00:05:23.695 --> 00:05:24.850
>> Thanks for having
me on the show, James.

00:05:24.850 --> 00:05:26.675
>> Absolutely. Cheers. Make sure

00:05:26.675 --> 00:05:28.925
you follow the Xamarin
Show at YouTube,

00:05:28.925 --> 00:05:30.425
Channel Nine. You know where to go.

00:05:30.425 --> 00:05:31.850
Hit that notification bell,

00:05:31.850 --> 00:05:33.725
become part of the
notification squad.

00:05:33.725 --> 00:05:35.390
I'm James Montemagno This is

00:05:35.390 --> 00:05:38.550
The Xamarin Show, and
thanks for watching.

00:05:45.230 --> 00:05:48.050
>> Hey, James here.
Just wanted to check in

00:05:48.050 --> 00:05:50.105
and thank you for
watching this video.

00:05:50.105 --> 00:05:53.195
Now, do all the things that you
know you want to do such as like,

00:05:53.195 --> 00:05:55.760
subscribe, and ding
that notification bell,

00:05:55.760 --> 00:05:57.770
become part of the
notification squad.

00:05:57.770 --> 00:05:59.150
While you're here, check out

00:05:59.150 --> 00:06:02.360
all these awesome videos
that I've already recorded.

00:06:02.360 --> 00:06:05.970
Click on that thing. Click
it. Watch it. Do it.

