C# Async / Await - Make your app more responsive and faster with asynchronous programming
Vložit
- čas přidán 2. 06. 2024
- Asynchronous programming can be intimidating. What is even worse is when you think you learned how to do something and then you try it in your application, and it doesn't work. This video is an attempt to fix all of that. In it, you will see how to use the async and await keywords to make your user interface more responsive and to speed up your code's operation. You will also see how to turn an existing method from being synchronous to asynchronous. Along the way, we will cover best practices and how to make sure our application behaves like we expect after we make a part of it asynchronous.
Mailing List: signup.iamtimcorey.com/
Source Code: leadmagnets.app/?Resource=Asy...
0:00 - Intro
2:23 - Demo application walk-through
4:33 - Code behind the demo application: Synchronous operation
10:36 - Creating Async Task
22:39 - Creating Parallel Async
30:51 - Recap
33:00 - Wrapping method in Task.Run() vs Async method call
35:42 - Summary
Mr. Tim, One of the advantages of C# is that you teach it.
I appreciate the kind words.
Tim, Thanks. This is the only "usable" explanation I found about async programming.
I am glad it was helpful.
sorry to be off topic but does any of you know of a method to log back into an Instagram account??
I was stupid lost my login password. I would appreciate any tips you can offer me!
@Pedro James thanks so much for your reply. I found the site thru google and I'm in the hacking process atm.
Seems to take a while so I will reply here later when my account password hopefully is recovered.
@Pedro James it worked and I actually got access to my account again. I'm so happy:D
Thank you so much you really help me out !
@Camdyn Valentin happy to help :D
I am senior .NET developer and I still use your tutorials and love watching you. One can never know enough. You are really awesome teacher and this tutorial on async/await is just what I needed. I know it is something what we are supposed to use, but not really have any real knowledge on WHY exactly. So awesome, especially with parallel.
Awesome! I am glad my videos are still helpful.
One can, but probably two can't
Just another student passing by to say thank you! Your slow and steady learning method is so much better than "Become a C# master in 5 minutes" type of videos
You're very welcome!
The best one that I've seen. Impressed how you can explain such a complicated concept in such a simple way!
Found your channel like a week ago, and I've been watching at least one video every day, and I'm learning a lot. Thank you Tim Corey, and you do make learning coding easier
Great to hear!
It is amazing how you wrestle complicated subjects to their knees with such a smooth narration that is so easy to follow and understand. Thank you!
I appreciate that!
Thank you Tim. I've been looking for video tutorials like this for a LONG time now. Your C# video tutorials are taught so nicely and you make it easy to understand. Most programming tutorials are either too fast or slow. Most videos have terrible audio but yours is beautiful. You are so helpful, please don't stop making videos.
I appreciate the kind words.
The best informative channel I've ever seen about programming... Thanks a lot sir!
You are welcome.
Even after one year, and this is still my best explanation of async/await in C#. Thank you, Tim.
You are welcome.
Great work Tim, I like the way you overview the basics first then come back & drill down. You are a gifted teacher.
I appreciate the kind words.
Thank you Tim, cristal clear!
Whenever I have a question during my course, I'll just search your channel, watch a video and BAM, question solved! Much appreciated!
I am glad my content is so helpful.
Best video on tasks I've seen so far. Not confusing in the slightest anymore.
Excellent!
DUDE! Thank you so much!! This was my third attempt trying get a working understanding of Async/Await and this did the trick! You are a baller!
Awesome!
Fantastic class. I've been studying this subject for a long time, but never found so clean and straightforward explanation. Congrats!!!
Thank you!
The relaxed, composed way of explaining this is absolutely sublime - I found the example code for delegates to be very complex; but here it was a treat. You also managed to handle all possible errors I already encountered before that other youtubers seemed to treat as evident. Kudos to you!
I am glad it was so helpful.
The best and clearest explanation of async/await I have ever seen. Look forward to watching some of your other videos.
Awesome, thank you!
i agree 200%! Well done!
Hey Tim - I know this is an old post, but still relevant and useful today. Well done! Perfectly succinct and easy to understand. I've been looking for supplemental teachings for our new team members that won't take a week to consume, and it was a blessing to stumble across your channel. Thank you for sharing and make it a Merry Christmas!
Glad it was helpful!
Tim, I can't say this enough. You are the best ever! You're videos are on a level of their own. No courses or videos I have seen explain stuff as good as you! Thank you for everything!
Wow, thanks! I appreciate that.
This video cleared up so many misconceptions I had around async methods and how to use them. Fantastic!!! Keep doing what you are doing because it is working!
Thank you!
Wow, that was incredible. I had just learnt the syntax of the keywords but didn't understand what they do and how they'd be useful. This video showed me exactly what I was looking for. Thank you so much .
Tim's goal is true education. As you point out, that is more than just memorization of syntax and keywords. We are glad you found the video helpful!
Straight to the point and easy to understand. You've got yourself another sub. Thank you for your time and effort put into this! :)
Thanks!
THANK YOU Tim! This is such a clear explanation on Asynchronous. It blew me away! To think I've been programming most of my life linearly (except for a few multi-threading things I've done). I am a new fan of your series!
You are most welcome. Thanks for watching.
Really cool that you showed how to run things in parrallell as well and explained the difference. Best video I have seen on async!
Thank you!
Hi Tim,I am in love with your teaching. The way you teach it becomes very easy to understand. Thanks for all your effort you put in to help us.
You are welcome.
Been diving in C# lately and I just couldn't get async/await/Tasks to click until watching this. Thank you for breaking each piece down and making them less intimidating to use
You are welcome.
I have watched this tutorial about 10 times now and I get more and more understanding each time as I implement async in my apps where some methods I have control over and some I don't. This is seriously a huge help! Thank you so much for making this!
Awesome! I'm glad it has been so helpful.
I'm a sr dev, and enjoy watching tech videos now and then, especially in the background as noise while working. I always enjoy your videos, recently discovered, as they provide different perspectives in how things are done or explained. I find explaining technical things like this to non technical persons a challenge, but you keep it simple, my motto, and that helps me to communicate with non technical peeps...
I'm glad you find them valuable.
Thank you Tim. I was desperately looking for enlightenment of async on C#
Glad it was helpful!
Dude, you're wicked. I always expect a video from that Indian guy but then you come through.
I'm assuming that's a good thing. :-)
Probably one of the best tutorials for async out there, if not the best.
Really good job!
Thank you!
You and your videos are fantastic. Learning async using the offical documentation felt kind of daunting and convoluted. You made it clear!
I appreciate the kind words.
Absolutly great tutorial. Finally I understood the async procedure. Thanks a lot !
You are welcome.
Excellent video. I'm fairly new to C# and found this video to be to the point, very easy to follow and the code to be highly readable. I learned a lot!
Great!
Really enjoyed the clear and concise explanation. I've seen asynchronous code many times but this really made things crystal clear
I'm glad you enjoyed it.
I just found your channel when looking for blazor information. It was a great experience. Most all other tutorials tell you click here, do this, then this, but never tell you why. You tell me why certain things are done, which is great, it’s how I learn best. During the blazor tutorial, you used async, tasks and await. I wanted to know more and found this tutorial, it explains it perfectly for me and easy to wrap my head around. Thanks. I’ll be watching more of your tutorials as I have time.
Thanks for the nice comments. I strive to educate, not just show examples. It does seem to be helping a lot of folks.
@@IAmTimCorey Do you have a video of setting up to allow login and restricting access to pages once you are logged on? This would also be in Blazor
0:00 - Intro
2:23 - Demo application walk through
4:33 - Code behind the demo application: Synchronous operation
10:36 - Creating Async Task
22:39 - Creating Parallel Async
30:51 - Recap
33:00 - Wrapping method in Task.Run() vs Async methhod call
35:42 - Summary
Thanks! I added it to the description.
Excellent video. I'm doing a course where we are shown how to make a web server and next task was to make it run async. I did that following the provided tutorial but that didn't help understand what is going on exactly. Plus it's always better to see it explained in a simple app like what you did. Thank you for this tutorial. For me 30 minutes is considered a short tutorial. 10-15 would be too short to understand and this was perfect and straight to the point. You explained everything in a couple of different ways and never strayed away from the topic at hand. It's rare to see such well structured tutorials. I will be back for more!
(this reminded me of an old game called Chessmaster, you explain things in a similar fashion to the Grandmaster (Joshua Waitzkin) who did the tutorials in that game)
Excellent! I'm glad it was so helpful to you.
I've been using these statements for 2 years now, and you just expanded my knowledge. Man, when I hit a demo where they start by creating a project I move on. Thanks for making every minute so full.
You are most welcome. Thanks for watching.
THANK YOU SO MUCH!!! Spent the whole night looking for this, just wanted a simple way of loading some internet data without blocking the UI and everything I found online was either not appropriate or way too complicated, thank you so much for the simple and clear explanation!
I am glad it was so helpful.
The best explanation of Async Await by far without a lot of fuss and confusing terms!!!
Thanks!
OMG been working at this on and off for so long and not figuring out what I was doing right/wrong. Thank you so so so much!!!! I completely understand now and see a productive week at work coming my way. Best tutorial ever!
Awesome! I'm glad it finally clicked.
I watched a few videos on this topic and for the first time I understand this concept. I like that you explain in details so newbies like me can wrap our heads around it. Keep up the great work!!
Thanks, will do!
Before I watched this, based on everything else I had watched, I just though Async methods always had to be followed with Await keywords next to the sub tasks within the methods even though they were completely unrelated sub tasks. And I wondered why my apps still ran "synchronously"! This is such a good video introducing one to asynchronous programming the right way! Thank you for taking the time to put this together! Now over to your advanced async video!
You are most welcome. Thanks for watching.
The best asynchronous programming video on CZcams!!
Wow, thanks!
Thank you so much. The only tutorial that made me understand properly, and I really like that fact that you explain that this is demo and not for production.
I am glad it was helpful.
Loved this! This was my first Tim Corey video, but I have to say he is the best presenter I've seen on the topic. He takes it easy, explains thoroughly, and somehow managed to explain EXACTLY the issues I had on mind after seeing other videos and reading some material. Many thanks!
Awesome! I am glad you found it so valuable.
You are brilliant in teaching sir 🙏🏼
So nice of you
As someone who have learned async as "This is how you do async in the code. Don't ask why just do it" I never really understood why. I understood that the program could run asynchronous but never really grasped why or rather how it ran asynchronous. After the 25 minute mark I got the Ooooh now I get it. Thank you so much.
Thanks for watching and trusting Tim to be a part of your coding journey.
same here. I have a solution full of external api calls database access, and they all "run asynchronously". I followed tutorials and troubleshot some caveats, but never really understood how to tackle async calls to make it actually efficient. Guess I have a lot of code to review now :)
This is in my head the video that helped me the most out of all I have seen on youtube. Learning asynchronous programming is well worth the time to learn and really makes things feel more streamlined and snappy. Also you gain control of when your tasks start and stop in a easy way.
I am glad you enjoyed it.
Hi Tim,
You're an amazing teacher, and I enjoy your tutorials very much. Thanks a lot and keep up the good work!
You are welcome.
Wow, this is pretty much like async/await and Promise in JS. The video was really easy to understand with that in mind.
Great!
Hi Tim, I am still quite new to learning C# specifically. I see there are some haters, but don't mind them. I appreciate your videos for not having a strange or annoying accent and also appreciate the real life implementations and examples as well as access to the source code. I appreciate that you take the time to explain things in detail and calling them on their correct name (eg. lambda expression). I also do not feel that you rush sentences or have trouble with articulation. Opinions are like a-holes, everybody has one. And that is just the opinion of some a-hole. Your content is superb and I hope to soon be able to contribute and take some of your paying courses. Thanks for your hard work, which makes my life easier.
I appreciate the kind words.
Best explanation about Async / Await I've ever seen! Thank you.
Awesome!
I was stuck on how to synchronise my tasks executions. Thank you! After watching this video, all starts to make sense. Thank you very much for your step-by-step and concise explanations.
You are most welcome. Thanks for watching.
I know this is unrelated but thank you for bringing that trick with the $ symbol with a string to my attention! That's seriously useful, since now I can clean up my code a little, get rid of all this
"some string: " + someValue + ", some other string: " + someOtherValue
crap. Also very nice explanation! I've yet to delve into asynchronous programming but I envisage this being extremely useful in my current project :)
I am glad you got some value out of the video.
Your voice and English accent are good. Few of vedios that not hurt my ears!Thanks!
You are welcome.
Also few comments that don't hurt my eyes 😁
Yeah great 🎤. By the way what model mic are you using I want to start making some tutorials
I like the recap at the end. I was expecting to have to go over the video multiple times to understand it, but the recap helped a lot.
Awesome!
A great, simple explanation of the fundamentals of a complex topic. Thanks!
You are welcome.
Great explanation and demo!
Thank you!
Best channel to learn c#. Wish I known it earlier
Thank you!
Great, easy to understand video. I like your pace and your explanation on each step. You made it look so easy. Till now I was confused when to use Task.Run() and when not to use. This video explained in a great way. Thank you so much Mr Corey.
Awesome!
I recently started following your courses!! Such a wonderful coaching.. thanks for your time and videos... Love you teaching from India.
Awesome! I am glad you are enjoying my videos.
I'm wondering... Why do i find this video only now??? Thank you so much. It was so easy to understand it with you
I am glad you found it.
This is so incredibly helpful. I absolutely love your teaching style. Thank you for this. If you don't already do Udemy courses, I think you should.
Thanks! I don't teach Udemy courses (for a number of reasons, mostly around control) but I do sell courses on my website ( IAmTimCorey.com ). If you join my mailing list, you will also hear about the new courses that are coming out and get exclusive discounts on said courses.
First time I watch one of your videos and I think it you have one of the best channels with programming content. It is very helpful.
Thank you!
Thank you Mr. Corey. That was absolutely the best explanation I've heard concerning asynchronous programming. I have enjoyed your videos very much. Once again, "thank you".
Awesome! I'm glad it was clear and enjoyable.
Best video on async await.
Thank you!
Great explanation, what i found interesting from what it appeared to me the order of the results came back in the same order for the Sync and Async. Just a bit strange but i really doubt you're doing anything weird, sure you'd agree order shouldn't be deterministic but just happened this way by random chance. But this was great explanation and above all don't want my comment to take away from how good the video is, just an observation, guess that's why i'm a tester at heart :)
Do you mean in the 'parallel' method? The tasks could have finished in any order. But the result array will contain the results in the same order as the Task[]. (So, tasks[0].Result will be in results[0], tasks[1].Result in results[1], etc) That's why the foreach loop will always enumerate them in the same order.
@@dennisekkelenkamp2303 Ah perfect, i see my mistake, thanks for your kind response, thiink i see my mistake. The list will contain the items in order as how you added them... (duh, shouldn't watch these at 1am when i'm not thinking straight...)
Think my erroneous thinking is a hold over to the old ways of treaing i.e. a thread pool and worker threads, that's me, wanting to over complicate it :) And that might be true under the covers in the state machine which .Net/C# is providing but we only have the reference to the object in the list Task, an they will be in "insertion order".
Thank for the clarification perfectly clear now :)
Have a GREAT DAY!
Nice job both of you. I'm glad to see you both contributing positively to the community. Thanks for watching.
Thanks for the video Tim! I finally got better understanding how to use it on practice. That's why I like your videos - you have a talent to explain quite difficult topics using simple, yet very detailed examples. Keep on doing your great job!
Excellent! I'm glad you have a better understanding now.
Stupid people talk about easy things complicated and smart people talk easy about hard things.
Finally a video that explains some of the different applications of async. Never understood it previously as I just thought what's the difference if you're awaiting on things.
I am glad it was helpful.
Six months of frustration, 25 mins of tim corey (sorry I watch at 1.5x :D ...) to solve it.
I am glad it was so helpful.
Great video, you explain things very well. I think, however, your parallel method isn't actually running parallel rather its running concurrently.
That is up to the system. It determines whether to run them concurrently or in parallel based upon how expensive it is to spin up a new thread vs how long it will take waiting on an existing resource.
@@IAmTimCorey ahh ok, I'm coming from Go where you have to write your code either concurrent or parallel. So you're saying the the .Net runtime handles that automatically? Rob Pike gives a really good talk on the subject.
Can't believe I understood ever word in the video! Thank you for the excellent work!
You are welcome.
Very well explained. This is not the first time Tim Corey has been very helpful
Thank you!
Thanks!
Thank you!
I’ve now watched a two digit number of videos covering this topic because I wanted to calculate the async way. Thanks to you my journey is finally over. Fantastic video!
Glad it was helpful!
Thank you so much Tim. These really helped me understand async await better than the other youtube videos. It really helped me.
Awesome!
Wichtig und richtig.
Thanks!
Hi Tim Corey !
Thanks for this video
Pleace !!!
Can you make lessons about 'clean architecture with rest epi in .net 5'
Step by step with Audentification and user admin roles ?
Thanks for the suggestion. While it isn't specifically "clean architecture" design, have you seen the TimCo series? In there, I set up a REST API in .NET 5 that uses Microsoft Identity (using Entity Framework) for authentication and authorization. Then I consume that API in both WPF and Blazor WebAssembly and make use of multiple custom roles.
Wow! This is simply the best Async tutorial! I managed to completely understand it despite being a complete beginner! Thank you very much for providing such amazing stuff for free.
You're very welcome!
Very clear video. Async is a little tricky at first, but once you get your head around it, it's simple to use & very effective on improving the speed of an application.
I am glad it was clear.
Hey Tim, Thanks for the insightful video. A question here, could you explain a bit more on why returning void in an async method (except in an event) is considered a bad practice?
Because the method cannot communicate a failure to you.
To expand on what Tim said, if it's void, you don't have the Task object, which can contain any exceptions that might have occurred while awaiting the task.
@@jonathanharmon3472 and @IAmTimCorey thank you for the clarification!
Hmm... how would i go about adding a try/catch in the async function?
Some of the listed websites sometimes send back an error - i added a try catch block to simply print "protocol error" in these cases, but in the async function that ends up crashing the program with "The calling thread cannot access this object because a different thread owns it." I assume that happens because my try/catch is in the WebsiteDataModel which isn't async itself?
Cheers!
This is how I solved this:
- Add property 'public string WebsiteStatus { get; set; } = "Ok";'
to class 'WebsiteDataModel'.
- When calling 'client.DownloadString/DownloadStringTaskAsync'
'catch' the 'WebException' and save 'webException.Status'
(and maybe 'webException.Message') in 'output.WebsiteStatus'
- Add 'data.WebsiteStatus' to the 'resultsWindow.Text' string in
'ReportWebsiteInfo()' to display it.
Thanks Tim for another great tutorial, you really know how to simplify confusing stuff and show the easy practical road for developing successful applications.
I'm glad I was able to cut through the confusion for you.
I've come across your videos before; But when watching this video, I was able to better appreciate your lecturing skills, since this is one of the more complex topics in programming. Thank you Tim!
You are welcome.
what are the potential benefits of using the native async implementation of a method (download website in this case) instead of just wrapping the synchronous?
Really good question.
There are a few. First, an async-native method can properly handle a cancellation token. That means you can cancel long-running tasks, stuck tasks, etc. You cannot do this with converted methods. Second, an async-native application knows how to interact with other resources while in another thread. Synchronous methods assume they are on the same thread as the UI so they may interact with the UI or other resources. That can cause threading issues.
God bless you. You have cured my confusion of async, await, and running tasks in parallel all in one go. I tried going everywhere: chatgpt, stackoverflow, google, and none of them answered all of my questions - ok except getting to you from google... You answered all of my questions and then some. Keep up the good work. Bookmarking this and going back to this video again and again.
I am glad it was helpful.
I still am confused: does the calling thread wait or continue while await is working?!
That particular method waits when you call "await" but it releases the thread back to the caller (usually the UI) to continue doing other work as long as that work doesn't depend on the results of the await.
@@IAmTimCorey Q1: the released thread will join other yet not used threads, thus decreasing the risk that the appl runs out of threads? Or, will it immediately be assigned to specific work?
Q2: given that the await’ed flow (fx of reading a file or performing a HTTP request) is without a thread, what ressource does it have? Reading from a file still needs processor power and RAM I suppose.
This is the best video tutorial on "async / await" I have ever seen!!! thank you so much Tim
You are welcome.
Because of your videos, I am getting better and better in C#. Thank you a lot!
You are welcome.
This video about Async was legen... wait for it ...daryAsync!
lol
One could say, await it
Very complicated. Thanks for making it, I appreciate that but if you are new to async this won't work as a tutorial. I think starting from a blank code will be better.
This basically was a "blank slate" project. The only things I did were to get a basic UI together so you didn't need to watch that and I created some basic code to call that was expensive. That "expensive" code can be anything you want and you don't even need to know what it does. The only way I could have done that with any less would be if I had built those two things on screen (but you would not have gotten anything else out of it since I explained both at the beginning), so it would have made the video longer but not added any real value to the topic.
@@IAmTimCorey I think you would have been better off explaining the logic before showing examples, actually.
Is it a good idea to measure time of the internet speed when wanne show the function of tasks
Really liked the video - a good starter for someone like me, who have just started exploring ansynchronus programming stuff!
Lifesaver! So many of the exampes for using Async were not directed at simply running your own functions on the side, you explained this very clearly and allowing me to add custom async pathfinding to my AI. Thanks!
Excellent! I'm glad you were able to apply it right away.
That moment while working on a project when you don't remember stuff and go straight to Tim Corey
I am glad my content is that helpful.
@@IAmTimCorey it is..
13 minutes to explain the overly complicated demo program before even starting to talk about async.
Context is important, as is not leaving anyone behind. If you are unsure of the starting position, you won't be able to grasp where we go.
@@IAmTimCorey Also you didn't talk about one of the most important points of confusion about using async. The dreaded "A method was called at an unexpected time" exception.
You don't get that message if you follow my instructions. That message happens because you don't properly await a call.
@@IAmTimCorey I felt that the context explanation was very helpful. Thank you so much for the awesome work you do 🙏🙏🙏
Sir, you're the best. I've watched like 5-6 videos about that but couldn't understand when we have to use await when not etc. But all of them clear now. Thank you for teaching! ❤
Awesome! I’m glad it clicked.
Awesome video Tim. As always, your educational skills are top notch!
Thank you for the kind words.
Much more clear than all of the examples I’ve seen floating around. Finally think I can put this to use!
Great!