Asynchronous Programming in C# Explained (Task.Run, Task.WaitAll, Async and Await)

Поділитися
Вставка
  • Опубліковано 18 лис 2024

КОМЕНТАРІ • 108

  • @sriaz81
    @sriaz81 3 роки тому +5

    Hi Frank Liu, Thanks alot for the video. However I have a little question. Is there any difference in parallel vs async programming? If yes, then is this tutorial is for Parallel Programming or is it about async programming?

    • @FrankLiuSoftware
      @FrankLiuSoftware  3 роки тому +33

      Hi Shahid, that is a great question! I am not sure if I can explain clearly in a comment, but I will try. To understand what asych is, the easiest way is to think about what synch is: task1, task 2 and task 3 have to run in sequence in other words, they have to run synchronously. Whereas, if task 2 runs before task 1finishes, we call it asynchronous. Here, we are emphasizing the order of executing.
      Then what is parallelism? Obviously, when two tasks are running at the same time, we say they are running in parallel. Here, we are not emphasizing the sequence, but that there are multiple activities at the same time.
      However, if you think deeper. In asynchronous programming, there have to be at least two tasks, the main task carried on by the main thread, and the long running task. The task is not blocking nthr the main task because they are running in parallel. So, they are essentially very closely related.
      Hence, I say that the difference between the two is in emphasis. One is empathizing the order of execution can go out of sync, the other is emphasizing two or more tasks can run at the same time.
      Because the emphasis is different, the problems the two are trying to resolve are different too. Asynchronous tries to resolve how to continue after the long running task, whereas Parallelism tries to resolve sharing resources/messages between the threads.
      This tutorial is more about async and less about parallelism because it deals more with how to continue after a long running task. However the two topics are closely related.
      I hope that helps.

    • @sriaz81
      @sriaz81 3 роки тому +1

      @@FrankLiuSoftware Thanks for the detailed answer. As per my knowledge, in async model, order of execution does not matter. It deals the problem of freezing or waiting when a long running task is executing itself. When we use await keyword, we are saying hey task please execute urself and when u r executing urself, give me the control back so that i can work on other things. So in this case time is switched b/w them for execution (they are not executing at the same time). When.long running task get completed it signals the main calling method that I am done. In case if we are doing async programming using task.run, then it pickup thread from thread pool to execute the task. While in parallel programming model, threads are created to execute the code and they all run in parallel

    • @FrankLiuSoftware
      @FrankLiuSoftware  3 роки тому +2

      @@sriaz81 Yes Shahid, you are correct. I worded it in a confusing way. What I meant by "the order of executing" is that after the long running process finishes, something needs to be executed. I was referring to the call back process. With task.run, we have to specify a call back, yet with async and await, we achieve the same result without specifying a call back function.
      Parallel programming has multi threads run at the same time (same as async), but here we are mainly trying to resolve the shared resources problem: how can the multi threads access the same resource without causing conflicts.

    • @rajesh35hsp
      @rajesh35hsp 3 роки тому

      async programming means long time taking tasks like getting input from user or reading from db, but not CPU intensive.
      While if you think of parallel programming it will be like doing some CPU intensive work processing millions of records.

    • @himanibhatnagar7439
      @himanibhatnagar7439 Рік тому

      @@FrankLiuSoftware easiest explanation.. thanks

  • @Jonathan-bn8hb
    @Jonathan-bn8hb 4 роки тому +23

    This is the clearest explanation video about asynchronous programming I've encountered. Thank you very much. You deserve a lot more subscribers. Keeps doing your thing man.

  • @os-channel
    @os-channel 4 роки тому +6

    Great job, never saw a better tutorial concerning basics of Tasks

  • @kopilkaiser8991
    @kopilkaiser8991 Рік тому

    You are absolutely brilliant. Months and browsing endlessly, you had finally made me understand the Task asynchronous topic. You earned a gold medal from me🎉

  • @NoOne-wf6hm
    @NoOne-wf6hm 3 роки тому +2

    Best video on UA-cam regarding Tasks for beginners.
    I still have to dig more into this until it makes absolute sense. Anyways: Thanks Frank.

  • @eprice000
    @eprice000 7 місяців тому +1

    Thanks for breaking it down in such an easy to understand way!

  • @一江春水向东流-y6q
    @一江春水向东流-y6q 3 роки тому +1

    this is really the best tutorial about async await c# for its simplicity and clearness i ever see, after reading so many microsoft documentation, i am still confused, now you save my ass, Thank you.

  • @naveenraj7503
    @naveenraj7503 9 місяців тому

    Thanks Frank for giving a detailed explanation.

  • @cicbeats2956
    @cicbeats2956 4 роки тому +4

    I consider myself as a slow learner, but this helped alot. THANK YOU!

  • @julianocs87
    @julianocs87 11 місяців тому

    Clear, concise explanation. Great video.

  • @andrewrayanit
    @andrewrayanit 2 роки тому

    Thanks man. Sat late night and have given us great tutorial.

  • @adhivenkatesh3431
    @adhivenkatesh3431 3 роки тому +1

    Hi Frank , Frankly telling its clean and very clear video , thanks for your effort and sharing to others.

  • @kasuntkarunarathne
    @kasuntkarunarathne Рік тому

    Thank you so much. Great explanation of Tasks, Async and await.

  • @marlonbraga
    @marlonbraga Рік тому +1

    Thanks Liu. This video help me so much

  • @jimbroiles6905
    @jimbroiles6905 2 роки тому

    Thank you for the clear explanation of the basics of asynchronous programming. I appreciate the way you that start with the basic concept and use very simple examples. Then additional concepts build on top off the previous ones, which builds our understanding step by step. It is an excellent approach to teaching. I learned everything I need to clear my understanding and go forward with correct implementations of asynchronous programming in C#. Many other tutorials on this topic should stop and learn from this one. Focus on teaching the concepts and implementation in simple, clear terms and quit trying to show how good you are at writing code. Many of us get lost in the complexity code you are trying to use as an example. It is a waste of time because the example is too hard to follow (Tim Corey please pay attention). I look forward to viewing more of your content. Subscribed.

  • @ukii261
    @ukii261 2 роки тому +1

    Thanks a bunch! Helped me out a lot.

  • @Nicetrycutiepie
    @Nicetrycutiepie 2 роки тому

    best explanantion and clean and simple to understand,, i just liked it very well...

  • @salaheddineelkhalifa8762
    @salaheddineelkhalifa8762 3 роки тому +1

    Hello Man, You saved my Day you nailed it. Many thanks !

  • @hardikbhatt6976
    @hardikbhatt6976 Рік тому

    Very very nice explanation

  • @cloudguy4192
    @cloudguy4192 3 роки тому +1

    Thank you for posting the video!

  • @jayronthompson2562
    @jayronthompson2562 Рік тому

    This is an amazing explanation.

  • @vindjedouatchomba7742
    @vindjedouatchomba7742 3 роки тому +1

    Thanks Frank Liu!! Great tutorial!

  • @LesibaPhokela
    @LesibaPhokela 4 роки тому +1

    Thank you Frank, you gave a best example ever.

  • @MasoudSamimi
    @MasoudSamimi 4 роки тому +2

    you are great teacher , it was so clear

  • @hiderr6805
    @hiderr6805 4 роки тому +1

    Thank you man! You definateely need more subscribers huh (EDIT: For a moment at the begining i thought it's a joke (those houses lol))

  • @santoshmandal6805
    @santoshmandal6805 2 роки тому

    Awesome............Well structured examples.

  • @Samuel-lm1wb
    @Samuel-lm1wb 5 місяців тому

    Awesome work! Thank you

  • @arborinfelix
    @arborinfelix 2 роки тому

    This was a real good and clear tutorial.

  • @nmarks
    @nmarks 2 роки тому

    Beautiful explanation.

  • @amolpawale1657
    @amolpawale1657 2 роки тому

    Very well explained. 🔥🔥🔥🔥

  • @Nicetrycutiepie
    @Nicetrycutiepie 2 роки тому

    just brilliant explanation bro cheers.

  • @olgaliuft5607
    @olgaliuft5607 3 роки тому +1

    You are a great teacher! Thank you for sharing these videos

  • @tamilstudent1
    @tamilstudent1 3 роки тому

    Such a calm explanation. Thanks.

  • @ramganapathy3298
    @ramganapathy3298 3 роки тому +1

    Nice video. However, in your Calculate1_2 () method, you have the method signature as, "async static void Calculate1_2 ()". It will cause deadlocks. Instead, it should be, "async static Task Calculate1_2 ()".

    • @FrankLiuSoftware
      @FrankLiuSoftware  3 роки тому +1

      That is correct. It is correct to use Task. UA-cam doesn't allow you to fix a video once it is uploaded.

  • @niroshanmanoharan4295
    @niroshanmanoharan4295 3 роки тому

    Thank you very much for your simple and clear explanation. May God Bless you!

  • @ofercarmeli6508
    @ofercarmeli6508 4 роки тому +1

    Very good demo. Thanks!!!

  • @BitcoinBabu369
    @BitcoinBabu369 4 роки тому

    Simply superb explanation. 🙏

  • @satishbhuktar2020
    @satishbhuktar2020 2 роки тому +1

    How your awaiter.GetResult( ) is providing value.. it's return type is void.??.

  • @ЄгорШамрай
    @ЄгорШамрай 4 роки тому +1

    Thanks for lesson!Can you make more videos about asynchronous programming or concurrency in C#?

    • @FrankLiuSoftware
      @FrankLiuSoftware  4 роки тому

      It was my plan to do a series of that. But I am still planning. Not sure which one will go next.

  • @242hiran
    @242hiran 4 роки тому +1

    Very calm and clear explanation. Thank you indeed Frank #maestro

  • @EraZz187
    @EraZz187 4 роки тому

    very good and clear explanation !!!

  • @MrSK168
    @MrSK168 2 роки тому

    Thanks for sharing!!!

  • @trustingod0
    @trustingod0 7 місяців тому

    Hi Frank Liu, Thanks alot for the video. However I have a little question. What is the difference between Concurrency and Asynchronous?

    • @FrankLiuSoftware
      @FrankLiuSoftware  7 місяців тому +1

      Check out my answer here: frankliucs.com/async-vs-parallelism-in-c

    • @trustingod0
      @trustingod0 7 місяців тому

      @@FrankLiuSoftware Appreciate you putting the link for additional clarity. But I still don’t understand why we have all of these wonderful ways of making a Task run asynchronously with the ability to await other tasks until they are finished but I can’t think of any use cases where this is applicable except for database and file operations.

  • @kanhamunjal759
    @kanhamunjal759 4 роки тому

    Very well explained. Thank you Frank !

  • @bioanu
    @bioanu 3 роки тому

    What is the reason you introduce the last function Test? I run the example using static async Task Calculate1_2() directly and the result is the same as using Test function.. What is the difference? Why do you introduce so many functions?

  • @ma8892
    @ma8892 Рік тому

    Hi Frank Liu. Nice Video :). In the end you are creating a Test() - method with "async static void Test()". Is this a misstake? Since the general advice is to avoid "async void" for async methods, other than for event handlers.

  • @avectresormt
    @avectresormt 3 роки тому

    clean explaination

  • @raunaksingh6050
    @raunaksingh6050 4 роки тому +1

    thanks a lot, please upload more...

  • @jorgesolano7060
    @jorgesolano7060 5 років тому

    Nice video man, thanks!!

  • @robiesun3713
    @robiesun3713 4 роки тому

    You helped me a lot. thanks for this video :)

  • @alijohn198
    @alijohn198 Рік тому

    love this video ❤

  • @anandtiwari4723
    @anandtiwari4723 3 роки тому

    thanks for this wonderful video :)

  • @markprescher
    @markprescher 4 роки тому

    Great video!

  • @mdabuzar2130
    @mdabuzar2130 4 роки тому

    Hi Frank, hope you are doing great.
    Could you please help me on how
    to process thousands of records in c#.
    Each record call a service to get some other details based on record id.
    Currently I am using paralell.foreach and it process 10000 record in an hour.
    Please suggest me how can I improve performance by 50%.

    • @FrankLiuSoftware
      @FrankLiuSoftware  4 роки тому

      Hi Md, the method you shall use depends on many factors. For example, the nature of the records you are processing, the way you are processing each record... I would do lots of comparisons and profiling.
      Have you tried without parallelism? How much time did that take? Have you tried partitioning the data before you do the parallel foreach?
      Just in case, don't forget to see if there are ways to improve the processing of each record.

  • @leozhang1340
    @leozhang1340 4 роки тому

    讲得好!优秀👍👍

  • @shuvbhowmickbestin
    @shuvbhowmickbestin 2 роки тому

    what's a GetAwaiter()

  • @zaffarullah_bhutto
    @zaffarullah_bhutto 4 роки тому

    Thank you man

  • @watchaccount
    @watchaccount 3 роки тому

    You did not exaplain the last part. changing return value to Task.

  • @Ramboo339
    @Ramboo339 4 роки тому

    Do you have an example with web api?

    • @FrankLiuSoftware
      @FrankLiuSoftware  4 роки тому

      What exactly you want to see about web api? I am planning my tutorials, you are welcome to give suggestions! Thanks!

    • @Ramboo339
      @Ramboo339 4 роки тому

      Using Asynchronous and synchronous methods for a web api weather app or news feed app for example

    • @FrankLiuSoftware
      @FrankLiuSoftware  4 роки тому +1

      Let me think about it! Thank you so much!

    • @UrvishKathiriya
      @UrvishKathiriya 4 роки тому +2

      @@FrankLiuSoftware Yes, I'd love to see that. A video on Web API async await will help a lot. Thanks. :-)

  • @БориславБорисов-я2ы

    Clearly!

  • @yz2039
    @yz2039 3 роки тому

    Thank you! Xiexie

  • @anushaaladakatti4145
    @anushaaladakatti4145 3 роки тому

    Good explanation, could have been a little faster

  • @FuzzyDPozzy
    @FuzzyDPozzy 4 роки тому

    9.32 was the reason i quit the tutorial.
    you should have kept it simple

    • @FrankLiuSoftware
      @FrankLiuSoftware  4 роки тому +1

      Thank you! I like constructive comments! Appreciated!

  • @fredrikterent8697
    @fredrikterent8697 4 роки тому +1

    Great!

  • @poojanaidu2396
    @poojanaidu2396 3 роки тому

    Screen is not clear

  • @_NguyenManhToan_
    @_NguyenManhToan_ Рік тому

    ❤❤❤❤❤q