 Before we start as well, anybody who has a phone like a new door open or whatever, is there somebody at the door? I'll try to have a look at the door. In general, but that might be just like that. Whatever before I start as well. Ah yes, let's come back right so well. I've just been awarded MVP for Beagle Studio and Malagis. So for everybody who wants to become an MVP, that's the good stuff to do, to start working on it. It takes some time and dedication, but that's worth it. Not for people, yeah, whatever. That's called a nice bunch of people. What else? Ah yes, that thing. Yes, so is anybody in the room an ISV? Yes, ISV, independence software, Panda. Who? You. You, me, me, I am one. Me, me, anybody else? If I say yes, we get this bottle of wine. Okay, they're going to prove it. Right, yes, if you are or if you have one, yeah, whatever. Microsoft has a program to accelerate independence software vendors, which is called app-excelerate, which is Microsoft.com slash dash slash app.excelerate, correct? Yes, app-excelerate. As it's written, probably here. So look, yeah, global platform. Oh, okay, a bit more to that, but that's it. So you might want to become an ISV just to be able to use it on your way. Do you know what ISV is? Yes, that's me. I hope you know what you're doing. I hope you know what you're doing. No, I don't know that. I have no idea. I just talk out of my deep all the time, which is just around the time today. I hope you just don't even forget it. It's just like acronyms are just boring to help. Just like the rules, but then I forget the method because it's not. Anyways, whatever. I hope you do write software and sell it. Yes, that's what you're doing. That's why we're here. Actually, just out of the crowd here, we've got an examiner in the room. One, two, three, cool. So I just have to ask you what the other guy is doing. Just like, yeah, just anybody randomly wants to answer a question. My idea was quite good. It's a little bit of wine. That's working. All right, let's get into the fire of the action of the topic of the subject of everything. Oh, yes, I need to do something before that, which is probably doing that in the background. Okay, yes. As per usual, I always do my presentation the last minute. And therefore, I just literally put it on the key drive in this computer, like about one hour ago. And like a bad person, I didn't download all the new packages on the solution that we're going to play with a bit later. That's normal. It's just like it keeps things interesting. Today, I think we'll be two presenters. I'll be myself. I will be talking about async await in the Xamarin contest. And then, as well, he's going to be talking about push notification in Azure. So like, like, I have notifications about other things. My name is Vanish Amalavi. What do I do? I've been doing Xamarin since it kind of pretty much exists. I love monkeys. I've got the company where we're doing purely Xamarin in Singapore because we love monkeys. That's all. And yes, I've been running this group like 4.4, almost three years of it. And yeah, and it's very, very fun. Cool. That's all right. Let me just do something like that. So you get packages in the background and I will go straight into that thing using async await. So even if you're not a Xamarin enthusiast, do you know a little bit about programming? Every time somebody just don't write the end, I will just like assume it's a yes. So everybody is just like top-notch programmers. That starts very good, very good. I'm going to talk there in like extreme technical terms, sometimes backwards, in order just to make it even more difficult to understand. For instance, instead of saying async, I was like neither. Or a way to be Taiwan. So that's going to be difficult to follow. But that's all right. Yep. Cool. Let's do it. So the objective for today for this presentation will be just possible to introduce you to the async and await keywords and how to apply the async and await keywords as well as diving into the actual internals for it. What we will want to do as well is just go through the history of asynchronous programming a little bit, at least in the net world. There's a wife who just spent like a week doing so and see how these two little keywords by the way that I've been adopted by a variety of languages since I've done that kind of with that, such as JavaScript, Java, what is it? Python is async and await. It's just literally going all over the place. And I really like just to go in the front seat. Oh, does Python borrow async and await for Microsoft? No, it cannot be. It cannot be. It cannot be true. I think this thread is going to be shut down because I've only Microsoft, so in all the good programming languages and all that's very, very fun. Anyway, that's for fun. I didn't know JavaScript had it. Anybody in the room use that JavaScript at asynchronous programming? Using async and await. Thank you, Microsoft. What is it? MSFB is great. Hashtag. Great. So, interesting async and await keyword on the blue background. So, oh, sorry. I just need to do something before that. Sorry, sorry. View, view, view, view, view. What is my present? No, I just want my present of you. Yes. Where has it gone? My present of you. I can't see it. Yes, it is being used, but I don't use it big. Are you understanding your own? Yes, I am, and I should not be extending. Actually, here we go. Use as a bird display. Yes, that's why. And now I can just go like this and F5 again. Yes. Okay, you have to stop this thing. Excellent. Excellent. Yes, it's going to be long, but I will go very short. So, look, why is it doing that? I am not... Why is it doing that? I need just to put it upside down or tell... I know what to do. I'm just going to open it on Mac, and that is just going to be the end of my problems. Yes. Luckily, I've got Power Point on Mac as well, and that should just go the right way. That. Sorry about that. It's just like it's useful to have some notes sometimes. Hold on. Is it putting the right Power Point? Yes. No. Oh, notes. Excellent. Okay, well, it doesn't want to... Oh, it wants to. It wants to. Come on. That is the right one. And now, so, View, my presenter should be there by default. Let's find that and see if that's good. Yes. I've got it. Very good. Sorry about this little thing. It's just like tutorials, but yeah. Whatever. So, we're going to be defining, obviously, what is asynchronous programming generally as opposed to synchronous programming, obviously. Also, explore the different options that are available for the .NET language. Like three of them, really, would be just good enough. Just to see how, by using this magic magic and the way this is going to help us a little bit to make our code more readable and more cool. So, what is asynchronous? It happens when something else is happening. Yes. Get the button off. Yes. Something happens when something else is happening. So, synchronous, basically, it's just like I understand something here now and then I have the video to do with this presser. It's going to say, right, I am just going to initiate something and then, well, we're going to ask a guy that was there before to be able to do something else or whatever. And then we'll see what happens. Basically, you just regain your controls immediately. Go. Which camp are you? Like on the red or on the green camp? Definitely green. Very asynchronous. That's the way to be. Leave your life asynchronously. That's, I don't know how that works, but that would be good. So, why are we using asynchronous programming? That's very simple, especially in the case of application. Application needs to be responsive. If your application is not responsive, it's going to be uninstalled. It's very simple. So, that means that your screen is not going to be locked and it's going to be frozen. More than 200 milliseconds is a quick start. More than that, that is literally going to be potentially uninstalled. So, make sure you examine it and you don't go ahead with it. That is going to help you out. Yes, it will. So, what are the kind of places where asynchronous programming doesn't work out? Whenever there is something that takes time. So, when you just do, for instance, read or write to a database or a file, which is not something that you necessarily need to write about. On the context already, you can write and do something. Context of reading, well, you just still want to read, but you don't have to freeze the UI loop, which I won't talk about a bit later in order just to get something back. Accessing the web service, like a recipe, that can be, obviously, long, sometimes in more than 200 milliseconds. So, therefore, make sure that, although if you design your recipe pretty well and you just have a lot of Azure distributed all over the world, then you'll be a bit better. Obviously, when you just have some background tasks or some kind of heavy CPU processing, such as desalination can be just a little bit time-consuming, for instance. So, as a good candidate for asynchronous operations. So, we're just going to talk about the real-life example of asynchronous stuff. So, when you go to the doctor, for instance, you go and see the doctor, and then you've got an appointment, but you still have to wait a bit. So, what can you do? You can, for instance, take a nap, which is literally, napping is a time machine, because you just, like, zip it up and then your future already wants to print. So, you can just either do that and go in the future and then have your appointment. But you need to achieve maybe if you're lucky a bit of proof, although in order to be able to get some sort of slumber that frequency that you represent, in order to be able to get dreams, I don't think that's been to approach the doctor appointment. But you could read a magazine, at least, like, if it's a good magazine or just a good book, a good book about, like the one that Mr. Sir is writing now. So, that's been nice, nice to read about Xamarin. Yes, yes, exactly. Read a good book about Xamarin and then you don't waste your time, your operations are started and then finish at some point in the future with a reason. It's a promise, right? Just exactly like in the real world, literally. So, you can wait in programming. Waiting is a default approach, which is just again synchronous programming. Why? Because it is, you will see a bit later, but just when you start getting into the asynchronous world until quite recently, you just end up working some code that is really linear and that is just a little bit confusing. Everything is backwards and levels up in every direction. It's just not great, right? So, just waiting. We're going to be lazy, let's wait. But it's not lazy, it's just like there is no really good way to do it until quite recently. So, it's easier to write at the end going synchronously. But it's not efficient because, again, especially in the context of mobile application that big UI thread, looking around, looking around, waiting for events, and I say, oh, I'm going to get some data from the web blocked, and that user can go like this and things are going to happen because it's just blocked by just getting some information from the web. So, we want to make sure that never happens. So, the loop of the UI thread loop is just always available for doing UI stuff. So, there are several asynchronous programming models to choose from. So, in the net 1.0, where this great stuff called APM is seeing for a main model, you probably remember it it just takes, like, two methods one which is going to be begging a single, and the other one is going to be and well, and making method and method. So, ultimately, begging the method was going to start just your process and you are expecting an async result which is going to contain what you need to get the result and these are the states of the async result. So, there are some drawbacks to this one that we are going to see. So, the good thing is just still going to be running in parallel, right? And the process is going to be taken on thread that is usually taken from the thread pool. So, that's kind of okay. And yes, and we pass the result to the end method in order to be able just to get the result from it. So, we'll go through an example later, but it means that you need to divide already your method into just, like, two parts. One which is going to be begging, which is just, like, I'm going to start this method and then I'm just going to do so. And again, you probably will have just, like, a, the good of the method is going to be from somewhere somewhere here with the three little parts instead of one. You know, that's better. So, then when the APM, the acing programming model, that just was really popular at the Silverlight time, anybody just put their fingers in Silverlight at the time? Yes, it was a dirty thing just like this. I'm pretty happy with that, literally. I think it's just, well, anyways, that's good to just. Nobody really liked it. It was just, like, something hybrid, just like, you know, it's a lazy way to do things, get something that is magic and is supposed to work out of the box. Not so good. WPF, yes, very good, Silverlight. Did you like Silverlight? There you go. Nobody likes Silverlight. So, I never met a single person and say, yes, I really like Silverlight. It's awesome. I've been doing, like, some great, I don't know, Pac-Man application with Silverlight. No, no, no, that's just, like, that wasn't between waiting for something great to happen, trying to compete with Flash. Flash is there, actually. There we go. Anyways, so, in this case, we're just going to take a single method in the APM with an event with an event callback. So, you just pass, it's just a delegate at the end, that is just going to be called by the time your processing is going to be complete. So, here we still have two parts, again, which is instead of one. That is, I don't think that it's necessarily better than the previous application. It's different. I think that something is missing. Something that just does the best of the buffers, but just, like, in very nice-to-read way. Also, using this method, because we don't have, like, an ISing result, you don't be able to get some reporting on the state of the actual process. And also, because you don't have this ISing result, you won't be able just to call back the spending operation together. Let's say you have an ISing course and you want to figure out what other state of all of those not to be able to sync on ASM, won't be able to do that either. So, not so great. And then we've got, like, the event-based ISing pattern, malarkey, buff. Now, the good stuff. The test-based ISing pattern, which is the one if you are a developer, you are probably already familiar with. It's been around four, I see, which went on in a certain year of the month or certain decade. A while back, it just don't keep track of the numbers anymore. But, yes, sometimes in the 2000 and something. Yes, so it just combined the two approaches, just a nice simple-to-use object, although it does have some methods that are highly misleading. But I'll come to that a bit later. We can wait it, we can pause, and we've got a callback. We've got three things. Awesome. So that is from now on the preferred approach for Microsoft good practices. Okay. Let's have a quick look at the original way of doing things here. That is hopefully my visual studio went well. Yes, let's try to debug that into an iPhone simulator. Up. There we go. So that's a simple application which is going to query... Oh, I can see it. Up. No, no. That's not going to work very well either. Okay, does work. Cool. Yes. Oh, I need to do... No. Okay, that's a bad idea as well. Use that built-in routine here. Like that. Yes, excellent. Tools, I'm just going to option... I'm just going to put it back to blue because that's kind of nice to program with, but it's just impossible to read for you guys, I suppose. There we go. We should have done that a bit earlier. I really don't like the theme. Just out of interest, who just programs on the dark theme? I'm not so good. You're waiting? Just like a... No. Well, anyways, you get used to it after a while. Just like about five minutes, I think. Right. So it's a very small examiner application that is just going to retrieve some movies from a iTunes. Malarkey. So if we have a look at the search result first, is that big enough? Yes, here we go. Let me just close that down a bit. Up. Here we go. So that is just actually some properties that are just going to be populated by a JSON call. That's just like a JSON property kind of, and then like nothing much more happening here. Then we've got a simple movie API which is going to... Oh, have I got the right stuff? Yes. Which is just like taking this iTunes stuff as a URL that is then just going to do a call to this API, deserlize that and then send this result to this guy there. Here we go. Which is just going to populate so we're taking like the results back from the API and then adding that to a list. So let's see how it works. This is usually the time why it doesn't work. So be prepared for something not working. There we go. Starts just to be annoying. Are you going to find me? In the meantime, I'm just going to spawn a Xamarin Android player because that might spiralite for a while. That might be a bit faster. Up. Some strong changes that none of those are going to work. Oh, I can see that that is disconnected. I don't know why it's not working. Huh? Ah yes, look at that. We're good here. We're still not good with this one. So I'm just going to go Android. So we're going to change that guy to that guy and we'll be fine here. Then I just need to be ADB connect and that's what 1071 1071 34101 something like that. That's it with the comma. Yeah, that's a nice comma just like to make sure that you're following. Good. You can have this. No, it's too easy. Okay, but that's it's not one yet. So every time I do a presentation here it's just usually like this. It's failing. Let me just do that and do that again. Okay, let's do something like ADB kill server ADB start server Let's do something else. Let's do like the full blown up. So usually when you've got parallel you do that, that action configure and you are going to change this connection from your network put it back on and now now ladies and gentlemen it's going to work. It does work. There you go. So if you've got the same issues that's the way you do it. Right. Now we can see that my little simulator there cannot really use the Android simulator from within my parallel because that's going to be high quality and that's going to be not good. Either not working or everything is going to be slow and potentially explode and the only easy way to do those days is just to get this Android player working that is kind of not being dated for almost a year though. Ah, here we go. Something failed. That's good. Let's build build solution. Why are you failing? It doesn't want to work, right? Build solution. OK. That's not going to be happening and my... OK. That's another thing just for the sake of doing it. Where you can get the simulator to work the iOS one is just to go here to tell this guy to disconnect forget me yes forget me, close that close that as well do that with your studio, reopen it and then then it's going to be recognized up and otherwise I would give up I would just say what it does and that would be just as good. Not really as good, but that would be just a fact. So if you are on the Mac and you have it, do you have it with Windows? Yeah, absolutely. I could go old Mac, but well, I don't. I mean just like Xamarin Studio is nice, but I really like to shop also a cool guy called Matt Robins does a good job with something called M-Fractor which is very good for Xamarin Form and Xamarin Studio. Now, ladies and gentlemen again let's get that thing that is loading here we are going to set that so no, it doesn't matter. OK, now we want to know about my thing. So app connect and now telling you just this bit like for for itself should be just like a little kind of presentation because that's the way you solve both the Android problem and the well, not solve in theory it should be solved then. Come on, you are going to be solved. Please, yes. Connect, no, not yet. Anybody's got like a bottle open now so I can wind them in time while you wait. That would be good. One, two. OK, well that's going to be anybody's got like a phone charger that's the other option. Ah, hold on. Success. Look at that. Connected somewhere. Success. So up, up, up. iPhone simulator. Yep. OK, well, yes. Excellent. There is indeed. You started. Blue card? Blue card. Yes. Blue could work. I think we're going to be working with that thing. That's built started. It's going to take like two hours and a half to complete. Up, up. Really? Why? Tell me why. Why did it fail? I don't know why. Oh, and low battery. A lot of why is happening today. So my this plug is not working somehow. Why did it be not working? No, no. It's just like a technical issues with... ah, here we go. I start to have a bit of juice. It must be a short circuit somewhere. Well, I am going to do some the demo right now. I'm just going to explain with code and if the gods of demo are with me a bit later I will be able just to be with you too. Although that is okay. Okay, let's do that. Actually, actually actually maybe with this guy it will work. But then you won't be able to see it which is just like a whatever, whatever. Okay. Thank you very much. So let's go through the code. What will happen if I launch the application? We will just type a query that is just going to query the iTunes API and we will get some movie results from it. What is going to happen though while the search is happening the user interface will be just doing nothing. You won't have a spinner but nothing at all. The reason why is because of the following. Yes, we are on the movie API. There we go. Why are you doing this? It's all very red. Why is it so red everywhere? It doesn't matter. That will not change how the code is. It just makes it confusing to read. Let me switch to Android. So the compiler is just like a bit happier somehow. Or slightly. Where was I? Yes, so here when we're searching we're just like sending a blocking call to the internet. We're just downloading, using a web client some query that was sending to an API which is this guy there. And while that is running we're not doing anything specific, we're just blocking. So the UI thread is going to stop spinning wait for that to open and then that is just going to release resume in order to run the rest here which is going to pass the result and ultimately present it to the UI. That's very nice because that's clear to read but that's also just not efficient at all. Let's see what we can do about it. Yes. I found nothing. All right. Excellent. What's happening here? No, no, please. No, that's something that's happening differently but I do have a black screen. We seem to be good now. So the problem with async programming so yes. Sorry, I didn't do finish what I wanted to do actually. Sorry. So it just takes a little bit of time for me to switch between one environment and the other. So here we go. Whiting. Where is my visual studio? I'll be here. Can I see it? Please. Finally. Yes. Cool. So one way we can solve it using like an old way of doing some async programming is the following. As soon as my computer comes back to be responsive, I'll be able just to show you. I'm just getting that a presence. Very nice. Well, yes. Here we go. Here we go. Come on. Show me things. So yes, if we want to actually do that an async way using one of the older patterns like that. First of all so that it's just like the async version of this guy there, which is the guy before, is literally just getting a web client and I'm just going to call this URL just get some data from it and then deserialize the thing, give it to the UI to populate a list. Here I'm going to do something different. I still just get like my URL here then I get my web client. Then the next thing is I'm going to say when we're finished unloading whatever we're going to download please just fire this event and what we're going to do is just get the results from this callback and we are going to deserialize them and that's great. Then what we are going to do is just say when the search is actually completed, please invoke that delegate and pass the results to it. So you also need to actually create here an action or some kind of delegates and then finally you are going to do a bit that actually downloads the actual data so download string asynchronously and you just pass your URL here. It's just like literally super upside down, right? You just first of all handle how the results are going to be processed in a delegate. You need to create an extra delegate on top of that and you finally just get just do your call which is basically a void call, right? It doesn't return anything because that's return because we're just again in a synchronous programming way. So the the first going to be access exactly this, exactly this we set up that, then we're going to execute this guy and then after we're just going to literally go back somewhere else wherever the caller was and then we're going to fire this guy. Yeah, I'm not a big fan of that at all. Furthermore, if we look at how it looks on the UI point of view, the async way we need to do a couple of things there. So first of all you need to do what we're doing a bit of UI malaki here we're putting a search progress simulator that is going to spin as soon as we start the process. Then after we need just to subscribe to this event, search completed and that is just going to call this method there. So that's a delegate we saw on the API and this is what we're going to do there because otherwise the UI would have no way just to know when the thing are ready, right? We're not again on a synchronous way it's just like the and the flow would not know what to do the only thing he knows is just like when we finish to download this data we've got a callback and this is where the UI has to hook in in order to be able to you've got data for the UI now you can fill up your data grid or whatever. So then after what we can do is just figure out if we've got actually some data returning and then you are hero handling here because again there is no hero handling on this just like asynchronous form it's just like you can do that it's not just building you can try catch, not possible. And then if you've got some data then you can just finally just populate your list with those items and then just say hey okay stop the spinner we're all good to go yeah so that is kind of not so good so again that was the original bit here synchronous which is simple shows the progress bar then gets the results and then just like stop it. So in order to run something asynchronous prior to the task a model or even the asynchronous way which is just a bit of sugar coating above it well that just it only is not straightforward you just have to write some code in a way that is just like a bit dirty you it's not easy to read it's a bit of a mind bender it's not sequential basically it's not great so there is no it's just like no wonder why programmers really prefer to go all synchronous because well that is just a bit simpler. Right that is going to work now hopefully I don't know it goes black for such a long time excellent yes on top of that as well just like in order just to remember your and your error handling just like end up having like some really really unreadable code it would be nice to have a little try catch simple nice efficient so so yeah prior to the nest 4.5 asynchronous programming was looking a little bit like what I just demonstrated no linear fashion and the processing of the result was completely decoupled from the actual invocation of the processing you wanted to do so it's just a little bit all over the place as we saw so again we are looking at an example of a non-linear logic which is great it's just like a simpler example to what we have seen here so just get the web client then you just set up your event look for errors here and then at the end very end do actually your processing which is there so how readability is called not really so yeah and I said that the processing code is just separately from this guy there exception are reported in non-traditional fashion which is a super simple try catch block and yes we are doing the processing last so here we just have a blocking port which is where our code is being blocked so at least here when we look at this code no this is just again the synchronous way to do it right it's just like oh let's know something then just that process whatever data we have and then just let's do something is great but it's not really asynchronous the problem for it so let's have a look at how to make the code simpler and we are just going to be looking at async and await keywords so that's going to be a better performance ultimately because you'll be able just to easily provide some asynchronous model to your mobile phone users by just having a UI that is responsive and it's going to be also providing a better use of the hardware because when on top of just not being very great for the users to hang around whenever you are doing a synchronous port that is also a way to CPU cycles that you could be using for doing something else so you want just to maximize the efficient just to do some backward processing to do whatever but just like to use what you got so there we go since t-shirt 5 we got the async and await keywords so wow that looks a bit whatever so with these two keywords yes we can let the developer develop the way they want to again they can literally develop on a synchronous way but actually doing some asynchronous programming although it's not all about those two keywords the two keywords as we will see will just simply be responsible for orchestration you still need to implement your your methods and whatever is going to be your process in a way that just makes sense it's not magic either it's just a facilitator right so when you decorate a method with this keyword async that will just tell the compiler that this method may really most likely to have some awaitable process happening it doesn't have to but we should have something but yes if you just decorate your method with an async keyword something will happen whether you want it or not something will happen then the processing code is defined exactly where it should be right after further fold together data which is nice and the error are handling handled in a try-catch block which is nice which is just a bit of a magic but we'll see how it's done behind the scene so it's absolutely perfect let's not look at that yet so let's go through that so let's apply async and await now so we're going to look at what does the async keyword do applying await keyword as well and working with awaitable expressions and also having a quick look at the limitations of this paradigm so what does it do? so async on the method implementation this is just what you defined the only constraint is it needs to be defined before the return type otherwise you can just use pretty much almost any modifiers would go through the exceptions and that would be cool we're going to bear in mind that async is that part of the method signature that is done at the IL level so that means that your interface your abstract class and all those malakis do not need to be equated with the async keyword it means that if you do have any just again base method that is not defined as being asynchronous it doesn't matter but it's not going to equate another comment you can do it yes and that's just pretty much that oh sorry yes so the reason why they've been using the async keyword has been used basically we'll see that it's just we provide directly to the compiler something to do with Microsoft didn't change the format to be able to equate the async and await reconstruct that is just something that is happening on top of the framework nothing has been changed so Microsoft which are usually really good to their developers don't tend to break their APIs and say have one also because I've just been writing the dust on Apple recently anyway so that is going to be used for backward compatibility because ultimately the compiler could sniff that we will have some awaitable methods within its body but that will just like make it future proof so the await keyword is a bit hidden because I don't know why the steps will be before that so that is going to say that there is an asynchronous operation taking place in this line that is ultimately going to download data from an endpoint you will notice that there is an async postface to the method name which is not necessary but that makes it easier as a convention to decorate your method with a postface async to make it easier for any developers or your team to know what is asynchronous or not without having to dig within the code so ultimately this await keyword will just tell this method that we are not going to get the result of that right here right now and then continue the execution we are going to do something and then we are going to give back the goal to the caller except if you use some modifiers we should talk about a bit a bit later so usually that is going to be as a session for some IO operation anything that is a bit time consuming CPU cycles just like again this realization for instance or also just like in other blocks such as on an iPhone you do an operation now are you sure you want to delete this thing yes no that is awaitable because something might happen in between there is it will only with you when the user press a button such as yes and no that is also awaitable so what does the await keyword do well the idea behind await is to post forward execution for the method after until the asynchronous operation is complete so we can see that we have got a clear separation here between the first bit which is we are doing some processing here and then we are just doing some processing there and then we are going to take the result of the operation and do something with it and ultimately behind the scene that is still going to be separated I mentioned with earlier that async and await is some sugar candy that has been important to develop as for them to be able to develop an asynchronous way the same way they will do in the synchronous fashion is just to make it just easier to read to maintain and so on but ultimately behind the scene that is still has to be separated and then we will use the second option sorry result is equal to await something only execute await and when the result is there then only ultimately whatever comes after is equivalent of continue if it is just like the same way it is just like continuation happens later on but ultimately we will see that in details but whoever is the caller of this method the one ever that is hit that is going to spawn a process if the result doesn't come that just like immediately then there will just the control will go back to the previous method which is going to resume execution usually in the mobile application that is just going to be the same UI thread that will allow you to put a spinner and allow the user to think around on the morning method we also have the await keyword before casting so if you go to that it's not always a case but you have to break the asynchronous chain but you have a couple of methods that cannot be sorry the decorator is an asynchronous such as for example structures properties or just like events but you've got some ways just to work with those that will get into the details by using tasks and then just doing your await construct within those tasks but yes we'll see that when you go await it's all the way so yes the compiler is just going to literally write that separately to that guy we're going to detail that is a bit mind-bending but quite fun yes just pretty much again just until that is done that is not to execute it's just going to go back to the caller so yes we can also use await on landers because ultimately the compiler is just going to generate from a lander an actual normal normal effort so there is absolutely no definition so the way you would do it is just again specify the async keyword before the parameters of your lander and then again just use the await keyword to just specify that this code is going to be awaited so that's no limitation here so let's have a look at what is an awaitable expression so usually in the async await world we need to have some kind of obviously a return from this promise and this return is going to be encapsulated in a task task is this guy this is a version of type nothing in some cases we'll see that we'll be able to use void but that is an example that I said before but that's not the thing you will want to do out of the cases that already exist because that is the open door to add bad things so I'll just want to have a bit of water so yes again usually having a task on your method definition means that portion of your body will be executed async mind you we'll see that later as well the method that returns the task doesn't need to be asynchronous because there will be some case where you actually want to have a task back return to the caller but yes usually most of the cases whenever you have a return value you will want to just wrap it inside the task so yep, that is a task in question yes, as I said before just like I mentioned prefix your method with the async keyword so yes there is a bit of confusion what async and await is going to claim the theme of the theme and that is not necessarily right that is just literally up to the synchronization context and especially depending on the way you are going to implement the methods that are going to be awaitable so that is not going to be a threat but crazy, it will simply just run synchronously as well it's going to be able to run in any fashion that the .NET framework will make sense of in order just to make maximum use of the code that you actually write and run it the most efficient way so something to know is await we don't know at all about the operation that is just that is prefix that is just going to work as an orchestrator behind the scene that is simply again just going to say just please, we are not going to execute what is after that until we've got some answer from this guy there but this await keyword will not know what is happening inside this method so yes, in this case here we just have this restaurant provides asynchronous capability here you are defining in this case a task that doesn't have any asynchronous body so it's not awaited so that will literally be processed synchronously if you are within an AC method as long as you don't decorate the method that you want asynchronously with the await keyword that is not going to go anywhere and that actually will be going to be looking at the UI when you are programming against user interface what you want to do the previous slide there is no await second method second method will be asynchronous synchronization the first method will still be asynchronous synchronization this method is asynchronous it's going to await on the task that is actually doesn't have any asynchronous thing happening so the first method will still be synchronous if there was a port below run world below the second line that would have been executed it will be I don't believe so now because here we are not awaiting on anything there is nothing to await on exactly the problem is also the time that that is going to run because it's synchronous it's going to lock your UI because of the cycle the UI thread is just waiting for that guy to finish and nothing is going to happen on this case again at an event which is a case that is happening usually you can have some events you can have the flexibility to override them with an asynchronous modifier but it's not always a case you can have some override or some method coming from an abstract class that are really not expecting anything asynchronous to happen and that will just break your application if you're not careful but yes the way to do it is pretty much that guy here let's say that this guy this work is implemented in a completely synchronous fashion which happens, you can work with MPI party libraries that you would like to work with in an asynchronous way but they don't provide any asynchronous method for if you go so this is how you solve this problem and just to run this piece of work within the task and now you're back into an asynchronous model anytime you're just locked with a third party library that doesn't provide anything asynchronous that's the way to go so because of the right work we use it after a while we have to decorate that as a thing we have to decorate that as a same task from work to be able to be used that's because that is returning a task right? in order to be able to use the task and then it kills the okay so if the one wouldn't be a thing then you wouldn't be using it away that bit is probably not a thing okay but I'm just saying so the run-off method because we're using it after the wait you're talking about oh this one yes so because we're doing it there we have to decorate it as a thing we have to yes but if you're awake that means that you you have to do that if you run it synchronously that means well I couldn't have a run-off to which is not a thing in order to use the task then I couldn't use it in a way okay maybe I cut back later okay for the second method can I decorate it as a thing on here? you cannot await a method that is not asynchronous so sorry okay yes okay cool flash quiz potential to win some wine okay which method definition is correct hey void async la la la void await stuff see void see see anybody's got something other than a see alright let's see so you can yes I can only give a top one when there is only one answer like one person that's too simple for now in response to the await key where the c-sharp compiler will create a thread true or false false why yes yes it is not necessarily it's just like up to whatever you've been doing yes actually for instance something that is interesting but literally like a write operation is writing to a file it's not on the open system something that is necessarily asynchronous at all yep so yeah summary we've been going through all that and that was amazing at both that's a nice picture right diving into the internals of async and await that's my favorite bit this is where we're going to look at the IL and see what the donut framework is actually doing behind the scene that's that's fun so what does the await keyword do we're going to explore the generated code and dealing with return values so at front time each await keyword style is asynchronous operation and then return to the caller because it cannot continue execution yet we've been through that a couple of times so await do something and then when it's finished let's go back to the to the rest of the body of the method in the meantime you went back to the caller yep so here we go path so here we just have we are starting an on tick malarkey we are going to start an indicator a spin up path that is running then we are going to await read from URL async while just getting to the method start instantiate our clients then we are getting into downloading something an aero operation what's going to happen there so the aero thread processes all the ui events while waiting for data to be downloaded so that's let's see oh sorry that's not what I wanted to do yes so we go back here after that has been hit and then we just give back the control to the ui right until something happens here and we've got some results once we've got some results that is done we are going to continue execution here we are just going to decode this data and return to do this operation then come back here and we are going to indicate that the spinner doesn't run anymore and then we go back again to the caller right in a that fashion as your ui thread that is just here we are going to read from the url asynchronously and then download the data async here from this moment here we in one side we are just going to be reading from the url so just a call to the api in the meantime the ui is just async we can play around push buttons do something great that is not a great application then we get back we get the data back from this guys and we just get the result and then we are loading the data and it's all great but how does a wait freely works so simply adding the async keyword is just going to change how the code is going to be compiled so let's have a look at something very simple just like hello world so this is on the il side of things your family we saw that on the il language that's also nice unreadable so we can see that we are just doing the td5 here and then we have the mscorlib that is just going to display a nice little hello world now let's have a look when we add the async keyword to smalaki or we've done so far we don't have an await here because that's not a waitable ui maybe you can put it in a task but that would not make any sense because you're talking as a ui anyways so now we've got something a bit more like a chunky here we can see first of all that that is we didn't create this thing like a d- minus 1d something like just something that we clearly created by the compiler and we've got a bit more stuff happening so that is sorry you've got that bit here that is generated by the compiler and we'll be able to hold the state on what is going to happen because we're going to have some steps that are going to be happening and the compiler needs to know what is happening on the multiple steps that are going to happen let's say something backward and forward and we can see here that there is no console write line in ui in this bit of code everything has been encapsulated into this move next method we're going to have a look at that I don't know why my stuff is a bit scrambled that was not this way but anyways it's just mostly gibberish anyway so that's okay that's okay ultimately this method always return after move next is finished so which is clearly true here we go we see something a bit better here so now we can see our hello world hello world malachi here which is just created in this move next as we think of so it's in this move next method so the move next switch like the step parameter and execute so switch is steps and execute different codes based on those steps so that means it's not really readable but that is just a very simple example that is just been a bit scrambled but ultimately we've got a state machine that is going to say okay are we running the process that we wanted to process asynchronously or are we just waiting for it to start or are we waiting are we ready to present the results and so for each operation like the state is going to change which is going to be 0, 1 and minus 1 1 is going to be oh we're waiting for something to happen and this is where the actual compiler is going to be done, the call to the execution to the caller and literally we'll just wait until this call has been completely white, we'll just get a notification which is an event that we'll be able to see I think in later on, yes to the current state of the async method but that's a bit gibberish, sorry yes we can see also here that's a part of the IL is just going to be capturing the exceptions on your behalf even if you don't want to even if you don't want to wish me that whether or not you have a try and catch block that is going to do something with this exception that will always just like capture whatever if they're not able to be able to present them to you in a nice fashion the task when I'm just like again like the other thing I'm still very similar to what we've been seeing in the first table, we just have still an uncompetitive callback which is just going to be called whenever the task has been completed you will have like some exception parameter which will be able to be written again just to the async await kind of construct at the end of the scene at the same it's just like literally a callback something to get your exceptions in and just as a parameter that is going to give you the state machine zero one minus one which is going to tell you is it finished to execute, is it executing or did it not start to execute and then after you can plug in some kind of then you can just plug in your cancellation tokens that would be able just to kind of work with those guys so under the covers the compiler turns the method into a state machine in anticipation of having portions be executed in steps so we've got something that is important as well we've got like local variables so if we've got like this bit of code here you are waiting to download something here I want that is going to be spun and just like to start the processing again we say that the call is returning to the method that actually called that here in the first place usually when you have some method, some parameters like result, data and your web clients define in the scope of a method what happens when you leave the scope of this method it doesn't exist anymore it's just been destroyed, they are much corrected goodbye so the compiler needs to find a way to actually keep like trial of those guys because we are still going to return to the color but we also need to keep track of the methods of the parameters that we've got in our scope in some way so that's what we're going to see so that's how the compiler just does it here in the scene it's just going to take those and put that into this state machine again and make sure that you have a hook on it until we return some until the process is actually complete so here it goes it's not scramble so one create a web client and issues that don't load async request which is with that so this is all steps here so that is literally loading the thing that is just a bit that is going to happen after that which is loading the data remember we said that the compiler is going to separate like the actual awaitable process to what is going to be run after so the continuity if you want that is literally what is happening here and that is literally the state 0 1 0 which is that is not the one that is going to be executing return and that is minus 1 not running so the compiler checks if the method is completed if not it's going to use a callback to hook to the callback with uncompleted which is where is my uncompleted somewhere and and it's going to go back to the move next when the operation completes if not already completed it's going to change the state of the state machine and so when the uncompleted event is invoked we are going to execute the next bit of the process and then we are going to return the move next to the caller which is just ok we started the process let's just move back to the method that is calling as the first string so in order just to not load this UI thread then obviously the UI thread will be able to summarize things and at some point the operation itself will be complete so the delegate is going to be just executed and yes we are going to move next again the state is going to be one and this is where we are just going to process the result of asynchronous for and the exception obviously are going to be called as for that guy then little quiz the await keyword causes the current thread to block waiting for the asynchronous operation to complete true or false one false one false two false I think that is everybody false now we got two right and let's have a look yes yes it is false I did an async keyword to a method definition does not change anything until the await keyword is used oh we got some trends of false excellent so it does not change anything so if I put decorate my method with async is not going to change anything it is false so just show a button exactly we saw that it was just like making a little bit of thank you on the hello world for some right line excellent what side effects does using async await always have on the method so just select all that applies the method would be broken into multiple steps exception would be cut and possibly rethrowed local variables would be captured and moved to the GC heap and it will cause a method to use multiple threads everything I mean for every single at least mention three or four times h a b a b a b c a b c d d not necessarily d not necessarily exactly everything the method will be broken into multiple steps then we have like two steps on the state step one step two one which is async call and then just return the color and then when the process is completed then we just get to the next step and so on exception would be cut yes we saw the try catch block and local variables would be captured yes I just mentioned that because otherwise if you don't keep those variables and return to the color then your context is lost and you won't have any variables at all so we need just to keep track of them excellent right okay so here we are expecting a byte right and this is what we are going to get from those guys but we know we have seen that from those guys we are expecting a test but here the result is a byte so that's it right here we go this is what we expect so how does it work again the compiler is just going to do the work for us behind the scene it's going to say all right one because the promise has been completed which is just like this test will have done some operation it's going to nicely give us two things one or the other it's going to give us what we ask of which is which is back to me or an exception so it's a yes of course but like any just like even in synchronous it's going to be the same right you are going to call like an asynchronous method and it's either going to throw an exception or you'll get what you want yes so quickly with the return value we've been mentioning that at the start so the return type of an asynchronous method must be void, task for task for type whatever which is what you expect return value, task which is normally when you don't have a return value for a synchronous method that will be void that will be a task because again the same thing you want to be able to know what is a stakeholder task you want to be able to continue with a task you want to be able just to configure the awaitability of a task, you want to be able to do a lot of sort of stuff with a task with void but nothing it's just like little fire and fog and you won't be able to if you've got some exceptions you won't get them your step is not being properly you won't know about it it's just like something to avoid you don't have the nice encapsulation that the framework did for you and it is just like super dangerous because that is it or you won't get anything it's like a big exception for lower anything can happen because that's pretty much what it does a task is just a fugitive we're going to get it eventually and that guy is going to unpack it for us so since our return method before the entire method is complete they must have a specific return type one of the three values as you went through it, task T if returns a value task from the return value void for event and loss really and constructors constructors constructors doesn't have a return type sorry so yes this is the bit about void you cannot await a void return async method since they don't return a task without an await you cannot catch any exception that you cannot ask you should never use a void return async method unless it is an event under or virtual method overwrite where you have no choice but once you get into it you don't make this mistake anymore because it's very simple what's going to happen is either going to be locking your UI or some side effects are going to occur and you've got no IDE so I can have a quick actually I'm going to skip that because we've seen enough example on how how async can await a work and so I've got the talk to do so I've got five minutes and I'm done so when is await a necessary so I went through that very quickly so if you do not need the process to I've got a small crimp on my foot that's probably because I need some water but I will not have any awesome wine maybe so if you do not need to process results from a task then you can just return the task directly so no need to await for it so for instance if you in this case let's say that well that's not a really good case but whatever if you don't need for the result you don't need just to if nothing is going to be processed and you don't expect the result to be doing anything that means that you don't need to continue the task with anything else therefore there is no reason for you to stone the process when there is actually nothing to continue the process literally there we go that's pretty much what it is yes, no now this is a second case so if you want to do something else with a task itself and you don't really care about the result right now you can actually result the task as well for instance if you want to continue something with a task or literally you're probably going to do some other operation that is going to be depending on the state of the task or you might want to kind of in the case of multiple asynchronous tasks that have been failed already and you want to synchronize them in some way you might also want to do that because again you've got access to the state of each task it's okay I just want to only return the result when both three tasks are complete otherwise I don't want to do anything that's the simplest way to do that but you may want to do this thing or that's also sometimes beneficial to continue the work on a task rather than switching back to the original thread so let's say that you're on a library not on the UI right and you've got like a lot of processing to do and you don't want to go back to the UI thread because that is going to load the UI what you want to say is say okay when I am going to be waiting on something I don't want to go back to the color just leave me in the context that this task has been just starting already so whatever the subsequent it's going to come it's going just to also work on the same context so basically you want to keep on the context that the task has gone on not on the UI thread so do we need to be back on the thread in the one when we run this code? Probably not so there is a trick which is when you write a library that is not UI dependent and that needs to be waitable I just really recommend you use FODI, you know FODI which is like an intermediate language whatever you might use it already in certain forms for the high property notified change like photo implementation that is just going to do that for you just decorate literally a class with this piece implement high property notified change for me and that is simply going at all time to look at every single public properties and it's going to inject some code in the IR I mean you can do a lot of things with it that would be pretty much the same because okay in this assembly every time I'm waiting on something please add that because in a library you don't have any UI thread you just want to keep going with the context you have because if you are just doing some processing and nothing to do with UI why would you just give back the code for UI? You don't want to do that so she says you don't give it back to whoever has a code just keep on this context whatever is going to be processing such UI that is going to be running on the same same context that's right basically I have three asynchronous tasks that I need to run all of them are not connected to UI at all if I don't do this what will happen is every time you just run that particular task then switch back to UI and then start the other one so there will be a jump between this and that there will be a jump and you also don't want that guy to be also running on the UI thread because that is available that is not it's only you just want to make sure that that thing is going to be running on the same context as this guy it doesn't have to be an available task after all it doesn't exactly the point it's not awaitable but you want to make sure that you run the continuity whatever is going to be executed after your awaitable task to be on the same context and not on the UI thread or just whatever is the color thread there should be a method similar to this to get back the UI thread it exists already but multiple ways to do that some that are very specific and some that are just like .NET portable specific web so in this example that is running on the UI thread then we are just downloading this Malaki asynchronously yes we are awaited so that is the task we are awaiting on this task and we are configuring the await to be not running back on the UI but to be running on the same task like I said that is the definition of the task that is the actual task and then this call here which is just going to encode the result is going to be running on this same asynchronous operation which is kind of nice basically that just ultimately makes that asynchronous at all in this context yes so if you got a task that completes immediately that doesn't mean that this one doesn't mean that the following task is going to be running on the same context because we already returned to the caller so that doesn't necessarily mean that all the way if you got an IOP operation so it would be fairly confident you would be on the same thread as the awaitable method so yes you can well that is just pretty much what we are manually switching from one context to the other the UI thread writes from the URL asynchronously and then we just configure these asynchronous processes with await false that means that we are going to have some UI activity that is still going to be running from the URL asynchronously but then the data instead before using the configure await false on the thread number one here is on the same thread as the awaitable test makes sense excellent, now just like if you don't do any UI work use a fully think, it does that for you you don't have to think about it so you have a good way to look at where the code is running which thread are there any tools to show you on which thread which process is running so you have a good thread number so you know which approach is running and it's just a bit critical because you always have more thread than you think you have and well, yeah that's it, but ultimately non-UI don't even think about it, that just goes on the same thing it's just like literally the only one to go back to the UI thread when you've got some UI process and so which method is await is on context yes the calling method is not affected by configure await so here what are we doing, sorry we are just reading some movies on some which is going to execute this method we have an asynchronous test as perfect by waiting on that guy and we are going to be running here with sealed web we are getting a task which is awaitable here which is configured with await as correct so that is on the same context that is sealed on the same context and then after we have done the caller we will go back into the main thread so just using again just like the configure await is not going to infect the caller it's just on the same yeah, yeah, yeah, absolutely yes otherwise yes it will be just like dangerous yes, so you said switching on the UI thread that's very important whenever you want to use a UI operation because adding stuff to the UI from whatever place adding some sales, navigating for example if you are downloading something and you want to update the progress of the download so if you are not on the UI thread obviously and you know people who are on the asynchronous context then what you do yeah that's right so what you do is you have a kind of cross-platform way to do that which is using the device of bigging invoke on main thread and then just use whatever you want but that is just like Xamarin specific so that's not going to be working on a PCL method that is not within the Xamarin context for that we've got something else, it does exactly the same thing that is using the synchronization context which is just like a couple of constructs and you just create your context and then you literally post the context but it's going to do the same thing so in general it's actually better to do that if you are like literally doing that on your UI I don't know but if you are going to be out of your UI probably use that because you might just share library with your API whatever for testing for instance if you want your testing framework to be able to deal with that as you don't have access to your UI you might want to use synchronization context quiz, now I think this one is difficult so whoever is going to be first on one of the questions that I deemed difficult they've got to win the monkeys are mine so the await keyword can be applied to any method called we want to run on a different thread be really fast code we want to slow down you want to slow down some fast code man method that return a task or task t or all of the above see yes no way to make too many answers just like that at this rate I'm going to keep the wine the await keyword should always be added to method that returns a task type true or false are you sure it's absolutely not true you can we just can use a yes the configure await keyword causes await to return on which thread the UI thread calling thread always a thread pull thread the thread that is the background work which might be the UI thread absolutely yes I especially did that you can do that in the library context so if you want to use a cross platform approach to switching back to the UI thread that should begin invoke synchronization context device begin invoke or sorry B yes absolutely that's okay and that's I've got like five slides to go through so coordinating multiple tasks I just want to have two properties just like let's say that you want to make sure that three or four tasks need to be completed before we move back to the next week or let's say that you want to you've got multiple tasks and you want to remove them one by one to do something some version as each of them completes so you've got an area of task and you want to do some impression depending on how many of them have been completed so you've got some constructs for that so you don't want to do that this way here if you've got like multiple tasks and you just go one after the other no that's that's that's silly because you won't be able just to process the results of them all because they don't know they'll be finished you need to do a bit of of stuff there so here you've got like multiple tasks and when you use the when all that means that you are waiting until all those tasks are complete and then you move to the next statement or which is just like as indicated here you are flight get flight run run run run run run run run run run all the tasks have been completed now we can do our stuff and I'm going like in fast motion if you've got yes multiple tasks and you just want to do something whenever a task is completed no way to do when any when any task is complete please do something and then you can just like remove any completed task and just keep on processing the rest and just have some processing done for instance if you were thinking about like a progress bar and say for like 20 tasks and every time that's finished you want to go further that's a good good use for those okay limitation all parameters needs to be put on a step magic on the state machine object right because we say that if when we are just on an asynchronous test we return the code the code to the caller and therefore we need to keep hold on our variable that are on the method scope so you cannot use out or ref parameters for instance because again out and ref parameters are not declared on the scope that are looking at therefore the compiler won't be able just to get all of them and say hey that's fine because it's in a different scope constructors and properties must return immediate value so it is also not possible so you're not going to be away anywhere near that things and yes you cannot look as well because in theory a log can just last a while and because what that happened it's just it's not going to be compacted just to make sure oh that's a log nobody entered this method until whatever that's not going to work you've got like some other ways if you want to do some kind of looking using a lot of semaphores but the logs are not going to be working also since C sharp 6 you can you've got a limitation for link queries I believe that on top of my head you can't wait on the first statement and on the first group statement and that's about it's not you cannot chain your awaitability I think that's about it okay done yes so it was a bit of context and content yes anybody has got any was it confusing enough that my like the beat in the video was a bit tremble open it on the Mac and then just open it on the window and then on the Mac and back on the window maybe it's something yeah is this useful what question is it allowed to have more than one one yes yes how would that statement be like doing that for each method so 1, 2, 3, 4 like that no 1, 2, 3, 4 is like per so that would be as much as you have to on it good so for a task.run you can put a week before and it's like the action you can what is the difference I mean like that's what it's just going to provide some asynchronous context usually you want to provide some asynchronous context to something that is not asynchronous so you could if you have a task.run you can wait on it because it's going to return a task yes it depends what you want to do if you want to just use a task for something else with it then just change it you can return it as a task and do something else or you can just wait on it and just return the value and the action inside the task on run you can wait on it as well yeah again you can change it no just again we thought that you just before your parameter set up your async you wouldn't then awake it sorry I'm switching to another channel thank you for yes yes thank you thank you for