What are ASYNC and AWAIT in C#? Asynchronous Programming Tutorial

Поделиться
HTML-код
  • Опубликовано: 22 июл 2021
  • 🚀 Master C# and .NET programming EASILY with our best-selling C# Masterclass: bit.ly/47Hk3u7
    What are ASYNC and AWAIT in C#? Asynchronous Programming Tutorial
    The world of asynchronous programming can be daunting at first. Using async and await in C# is a proven way to write code that does not block the main thread, which means it will scale better and be responsive even when dealing with long-running tasks such as IO operations. This video will cover what ASYNC and AWAIT are, how they work, and best practices for using them together.
    Asynchronous programming
    Asynchronous programming is a means of parallel programming in which a unit of work runs separately from the main application
    Normally, any code runs straight along, with only one thing happening at once(line by line). If a method calls another method, it has to wait for the other method to finish and return, and until that happens, the entire program is essentially stopped from the user's perspective. Even the UI would freeze and look unresponsive until the task is done.
    This is where Asynchronous programming comes in handy.
    Asynchronous programming is a means of parallel programming in which a unit of work runs separately from the main application.
    Now when should we make our apps run tasks in parallel?
    Let’s look at the following real-world scenario from the MS Doc.
    Imagine we are cooking breakfast.
    We want to make coffee, eggs, and bacon with some toast and jam, and some juice to wash it all down : ).
    If we wait for each task to be done to move on to the next task, it will take around 30 minutes. But if you think about it, we don’t need to wait for the eggs to be done to fry the bacon!.
    This is an asynchronously prepared breakfast. We can actually speed up this process and optimize it using the Asynchronous method.
    Now using the Asynchronous method.
    We can first pour the coffee, then start with frying the eggs and the bacon and turning on the toaster at the same time!.
    Once the toast is made, we can put some jam on it, pour the juice, and the breakfast is ready!.
    So tasks that do not depend on other tasks like frying the eggs and the bacon can be started together. Tasks that depend on other tasks like putting jam on toast must wait for the toasting step to be done. Of course, don’t forget to take off the eggs and bacon, so you don’t burn them ;).
    This whole process will take less than 20 minutes, thanks to the Asynchronous method of preparing breakfast.
    So how can we apply this approach to our programs ?.
    .
    tutorialsEU offers you free video tutorials about programming and development for complete beginners up to experienced programmers.
    This includes C#, Unity, Python, Android, Kotlin, Machine Learning, etc.
    Stay tuned and subscribe to tutorialsEU: goo.gl/rBFh3x

Комментарии • 63

  • @tutorialsEU
    @tutorialsEU  Год назад +1

    🚀Master C# and .NET programming EASILY with our best-selling C# Masterclass: bit.ly/47Hk3u7

  • @Rodioser
    @Rodioser Год назад +8

    I struggled, and I'd say that I'm still struggling with understanding of how async works. But surely I'm making progress in understanding it and this video was a big help. For me it's the best explanation of async that I've seen.

  • @aricwilliamsdeveloper
    @aricwilliamsdeveloper 2 года назад +7

    this guy is an incredible teacher

  • @Felipe-mg1pw
    @Felipe-mg1pw Год назад +20

    You should definitely not be using Thread.Sleep() inside an asynchronous method. Use Task.Delay() instead.

    • @BBTRaziel
      @BBTRaziel 11 месяцев назад +1

      and don't forget to await it 😉

  • @GurtMan100
    @GurtMan100 2 года назад +8

    Super useful tutorial! Been trying to get a grip on async for a while now, and this really helped.

  • @kopilkaiser8991
    @kopilkaiser8991 Год назад +4

    The other videos on youtube explaining asynchronous programming is much more difficult to understand. But you have been able to explain this in a more simple and easy way. Thank you for making such great effort. I hope you have a great day and achieve much more success in your life being able to gain more knowledge across all areas.

  • @quicktastic
    @quicktastic Год назад +2

    The key to this being good is in using a simple example. Nice job.

  • @frosky9497
    @frosky9497 3 месяца назад +1

    For people that still dont get it , how it works codewise
    basically whenever a current debug line (this is simplified) meets await keyword action is forwarded to the code straight after that function usage
    which had await keyword inside it, if there is no function before that code simply waits.
    when action finishes another "executor" comes back and continues execution from the place await was setlled in.
    Keep in mind program can finish before that action completes so its always important to wait for the task (unless its fire and forget like write to a file in a UI app!)

  • @os-channel
    @os-channel 2 года назад +2

    step by step, great!

  • @ahadporkar7355
    @ahadporkar7355 2 года назад +2

    Very Simplified and nice explanation

  • @getsmartpaul
    @getsmartpaul 2 года назад +4

    Thank you for this excellent coding example. It’s educational how you show the most basic first and progress to a more useful example!

    • @getsmartpaul
      @getsmartpaul 2 года назад

      I want to apply this to a asp.net Mvc file upload and a long processing of the file data example. I assume making the processing asynchronous will minimize blocking the main MVC thread ?

  • @AllAboutDataTechnology
    @AllAboutDataTechnology 6 месяцев назад +1

    this is the best tutorial on async and await that I have seen. Clear and consise examples given.

  • @MikeFlyItAll
    @MikeFlyItAll Год назад

    You're a good teacher... thank you and cheers

  • @maiksonstrife
    @maiksonstrife Год назад +4

    This kinda of tutorial should be default, it's the best one.
    A lot of other tutorials abstract the lesson in 50 hundred layers of abstraction, the person that is trying to teach seems to be conflicted: should I just teach this in the best way possible or should I show off my awesome coding skills?

  • @ssbunnies2015
    @ssbunnies2015 Год назад

    good one, simple and crisp

  • @AlexxXRecorD
    @AlexxXRecorD 2 года назад

    Thanks, very interesting.!

  • @kopilkaiser8991
    @kopilkaiser8991 Год назад

    This video was a great help and a step up in reaching to grasp the concept of this complicated and difficult topic in C#

  • @couch_coach
    @couch_coach Год назад

    perfect explanation, thank you

  • @efrenfeliciano5150
    @efrenfeliciano5150 Месяц назад

    Best ever explanation I have watch.

  • @sathishraj1
    @sathishraj1 Год назад

    Excellent tutorial

  • @raven.4815
    @raven.4815 Год назад

    Goddamn, someone that finally explains it in a non flustered way. Thank you so so much!

  • @sunilGMI
    @sunilGMI 9 месяцев назад

    Thanks for making a wonderful Video on Async and Await

  • @stephenwood4814
    @stephenwood4814 2 месяца назад

    This is an excellent video. One nit: you use the word "parallel" early in the video, then again about 14 minutes in. The correct term here would be "concurrent", I believe, since it's important to distinguish between concurrent (single worker thread, async/await) and parallel (multiple worker threads, Threadpool e.g.).

  • @vagrantrandomstuff2312
    @vagrantrandomstuff2312 Год назад

    Very informative sir. Thank you

  • @TutorialMax9812
    @TutorialMax9812 2 года назад

    Hey Dennis,
    leider habe ich bei der Ausführung des Codes Compilerprobleme bezüglich der Main-Funktion.
    Ich verwende wie du "static async Task Main()", leider ist Visual Studio 2012 der Meinung, dass das keine geeignete statische Main-Methode ist. Ihm fehlt der Einstiegspunkt für das Programm.
    P.S.: Ich habe deine C# Masterclass durchgeführt und der Kurs war super! Schade, dass ASYNC kein Teil davon war.
    LG Max

  • @siddarthapal2792
    @siddarthapal2792 4 месяца назад

    Simple examples to explain with graphics helps. Great work buddy :), god bless

  • @martorulez
    @martorulez 2 года назад

    brilliant

  • @mikicerise6250
    @mikicerise6250 Год назад

    Question: If your main thread crashes after having spawned async threads, what happens? Do the threads close? If not, what happens when they try to return their Task? Is there a way to do an elegant shutdown of all existing threads using a destructor in the parent thread?

  • @mermaidoasis8452
    @mermaidoasis8452 17 дней назад

    Could asynchronous programming be used inside for loop with nested if statements or would it be useless? Thank you so much.

  • @Charles_001
    @Charles_001 7 месяцев назад

    thanks for your vieo it's best one

  • @NHGeneral
    @NHGeneral 6 месяцев назад

    I have a program like this:
    1. Have a static string[] 'values'.
    2. Task 1 is event driven, update the content of the string[] 'values' whenever condition meets.
    3. Task 2 is periodically use the data from 'values' array to display status of the equipment.
    Please help me how to implement this into the project.

  • @GuidoSalami1
    @GuidoSalami1 5 месяцев назад

    is using httpclient how you get the browser to open up a url? does it work without any browsers installed on your machine?

  • @spartanAXA
    @spartanAXA 9 месяцев назад

    Now let's get started with the tutorial

  • @doointhedoo
    @doointhedoo 8 месяцев назад

    I'm an American and this tutorial made me hungry and want breakfast. :)

  • @mohamedslimen1041
    @mohamedslimen1041 2 года назад

    hi your work is very good am mobile apps dveloper and i have a game idea : is about a 2d mutiplayer game it can be just between 2 player i ilready developed it work perfectly localy but i want it to be in network like ludo king i need your help in a tutorial wich server is good for it ana does firebase realtime database is good for it or no i found many solution like foton and mlapi server please try to find a good solution for it it can be like ludo king help me please thanks

  • @BeansEnjoyer911
    @BeansEnjoyer911 2 года назад +10

    now lets get started with the course
    now lets get started with the content
    and now lets get started with.. the tutorial
    nice poem!

    • @NeonGreenT
      @NeonGreenT 2 года назад +2

      I thought he was trying to hint at curly brackets, like:
      namespace bla
      {
      static bla main()
      {
      static void methodbla()
      {
      nowWeTalking();
      }
      }
      }

    • @day35ofdebuggingthesamelin56
      @day35ofdebuggingthesamelin56 Год назад +1

      I think he was just making a few short intros and outros so that he can use them for many other videos, but forgot to edit that out lol

    • @NeonGreenT
      @NeonGreenT Год назад

      @@day35ofdebuggingthesamelin56 no that was def. on purpose

  • @Lonchanick
    @Lonchanick 4 месяца назад

    Thread.Sleep does not block the thread?

  • @alexpato4
    @alexpato4 3 месяца назад

    The async keyword DON'T specify that a method is asynchronous, it just starts the state machine for the method. In another words, the async keyword just prepare the method to do some asynchronous stuff.
    You can have a method that returns a Task and are not marked with "async"

  • @johnmullally8711
    @johnmullally8711 Год назад

    I get CS4014 in .Net 6.0 & VS 2022.

  • @ChrisWard74
    @ChrisWard74 5 дней назад

    Your first diagram shows "heating the pan," but your second one does not. You should have shown a comparison of doing it simultaneously as well.

  • @sihlejali8453
    @sihlejali8453 2 года назад

    What if you only have one pan? :)

  • @gabrielreinfalke2277
    @gabrielreinfalke2277 Год назад +1

    Example is good, but the theory as explained is wrong. For example, a void method is a void method and a Task method is a method that returns a Task. Returning sth. is indeed different than returning nothing or sth empty as null.

  • @ramonstanlyrodriguez4189
    @ramonstanlyrodriguez4189 2 года назад

    I coded the same thing and is not working for me why is that ?

    • @barnabas_666
      @barnabas_666 2 года назад

      maybe you misstyped URL adress, be carefull with letters like '1' and 'l'.

  • @znefas
    @znefas Год назад +1

    As many have pointed out, this is not a good tutorial which actually explains asynchronous programming, it's just a display of some pre-existing async functions, which to most developers worthy of being called such, is trivial and not very useful on its own.

  • @trustingod0
    @trustingod0 5 месяцев назад

    Unfortunately it’s not possible to make a video tutorial for a one size fits all. It’s a good tutorial if most of the audience can walk away saying they understood the concepts.

  • @adolfomedina1774
    @adolfomedina1774 2 года назад +2

    i still dont understand, damn

  • @74himgup
    @74himgup 2 года назад +2

    sorry but your example makes no sense to me. What is the advantage, what if you do not use async and await...you did not explain anything properly....

  • @andrebarendse177
    @andrebarendse177 7 месяцев назад

    INTPs and Programmers shouldn't make jokes... Just putting it out there.

  • @funarfiif982
    @funarfiif982 Год назад +15

    Very poor video. He basically explains the syntax (which is trivial), but is not showing comparing executions of sync vs async or demonstrating the order of execution depending on method. Literally useless if you want to understand intricacies of async/await beyond basic use instructions.

    • @znefas
      @znefas Год назад

      totally agreed. there's no need for a video that just shows the async/await syntax, but doesn't show how to, for example, make your own asynchronous functions, and doesn't even show a thread that continues running, and the order of the function calls being less relevant.

    • @trustingod0
      @trustingod0 5 месяцев назад

      Is there a better video tutorial for understanding the intricacies of async/ await. Thank You ! You might try watching a Tim Corey video where he makes the comparison but that doesn’t make it a better video. The instructor for this current tutorial is extremely great instructor.

    • @nafiaus
      @nafiaus 3 месяца назад

      thanks for the warning.

  • @sidali9272
    @sidali9272 2 года назад +11

    Not worth watching

  • @laythabdulkareem1887
    @laythabdulkareem1887 Год назад +1

    Dude, you need to enhance your presentation skills 👎👎
    You are so fast!