[Tin tức] Making async code run faster in C#



Become a Patreon and get source code access:
Check out my courses:

Hello everybody I’m Nick and in this video I am going to show you how you can detect use-cases where you can make async code perform faster. The usecase is relatively nieche but common enought that you might be able to use it in one way or another.

Don’t forget to comment, like and subscribe 🙂

Social Media:
Follow me on GitHub:
Follow me on Twitter:
Connect on LinkedIn:

Keep coding merch:

#csharp #dotnet #async

41 bình luận về “[Tin tức] Making async code run faster in C#”

  1. You're a hero. I just started using c# to make a web app using blazor, and have been using the html5 canvas which has all of its functions asynchronous.

    I was thinking having await on every single function call is gonna mean it's basically just running sychronously, but surely they could be run in parallel. This is literally exactly what I was looking for.

    Bình luận
  2. Thanks for your videos man we always learn a lot from you. I've tested WhenAll on a large list, it does not execute them together or faster at all though. It just lets you know when all the tasks have been complete.

    The only method that made the task run in parallel is the good old Parallel.ForEach method.

    Bình luận
  3. @3:17 you mention those tasks won't start at that point. I thought they would start but all almost at the same time? The task object returned would just represent the work being done. await doesn't start them but just doesn't let execution continue past that line until the task being awaited is done.

    Bình luận
  4. One thing to note is that the child methods do, in fact, run immediately and only return to the main method once something is actually awaited in the child method.
    Try playing around with something like this:
    async Task main()
    {
    var t1 = Child();
    var t2 = Child();
    var t3 = Child();

    await Task.WhenAll(t1,t2,t3);
    }

    async Task Child ()
    {
    Thread.Sleep(1000);
    await Task.Delay(1000);
    }

    This will take 4 seconds to complete… I have seen this many times when people try to use async to achieve parallel code. If you truly want the execution to be parallel then you cannot use this method, you must do something similar to:

    var t1 = Task.Run(Child);
    var t2 = Task.Run(Child);
    var t3 = Task.Run(Child);

    await Task.WhenAll(t1,t2,t3);

    This will then return in the expected 2 seconds…

    Bình luận
  5. WhenAll – This is what I've done when a customer asked to check if we can make it work faster (the request was processing 10 seconds). Reduced to 8 secs. Yes, it was not so helpful, so later we had just added dynamic messages on UI, to show a user that we are actually doing something important. "Checking this, loading that, calculating, etc".

    Bình luận
  6. A nice video showing quickly an easy parallelization win with async. My Q is how to implement task cancellation with WhenAll? With a naive implementation, the WhenAll task won't throw the taskcancelledexception until it is awaited (maybe that's the right behavior, idk)

    Bình luận
  7. Hi Nick,
    Thanks for the great explanation. I tried to use this (Task.WhenAll) in one of my projects, the thing is I would like to read some data from DB in each async method. But what I get is an error that in .Net Core we cannot use the same DBContext instance to call in parallel asynchronously the DB. Is there any best practice for this? Should I use different instances of DBContext in each async method? or at all this topic is not relevant for DB scenarios?

    Bình luận
  8. This seem like you are just going to push the performance cost somewhere else. This particular call will be faster (you don't have to wait for a thread to free up to run each await), but whatever thread you would have had to wait on, is now going to wait on this indirection. I suspect if you ran a concurrent workload (e.g., a web app with more than one request/second) you would not see any significant performance gains.

    Bình luận
  9. I were to high school and there i learn that the good language is c, after time i learn that c not is use usually, than they recomended i learn java se, paste time i learn that java web is more popular than they will recomended i learn java web, still they recomend c#…

    Bình luận
  10. About .Result vs await:
    Await is more fool-proof than .Result, because if you will forget to await one of tasks in Task.WhenAll(…), then it will not blow into your face.
    Also, you already have a state machine (when doing that await Task.WhenAll) so I don’t think there will be big difference if you will add few more states there.
    So, I think that await is still better here

    Bình luận

Viết một bình luận

bahis10bets.com betvole1.com casinomaxi-giris.com interbahis-giris1.com klasbahis1.com mobilbahisguncelgiris1.com piabetgiris1.com tipobettgiris.com tumbetgiris1.com betboro 1xbet giriş
bahis10bets.com betvole1.com casinomaxi-giris.com interbahis-giris1.com klasbahis1.com mobilbahisguncelgiris1.com piabetgiris1.com tipobettgiris.com tumbetgiris1.com betboro 1xbet giriş
antalya bayan escort
antalya bayan escort
antalya bayan escort