Await Async Tasks Are Getting Awesome in .NET 9!

Поділитися
Вставка
  • Опубліковано 8 кві 2024
  • Until the 21st of April, buy ANY Dometrain course and get the From Zero to Hero - LINQ in .NET course for free!! dometrain.com/courses/
    Become a Patreon and get special perks: / nickchapsas
    Hello, everybody, I'm Nick, and in this video I will introduce you to a brand new await async and Task feature coming in .NET 9!
    Workshops: bit.ly/nickworkshops
    Don't forget to comment, like and subscribe :)
    Social Media:
    Follow me on GitHub: github.com/Elfocrash
    Follow me on Twitter: / nickchapsas
    Connect on LinkedIn: / nick-chapsas
    Keep coding merch: keepcoding.shop
    #csharp #dotnet

КОМЕНТАРІ • 206

  • @tehsimo
    @tehsimo Місяць тому +59

    Lovely feature
    BTW that zoomed middle region messes with my brain

  • @DredTather
    @DredTather Місяць тому +18

    For concurrent processing, there's also the channels library that's been available since .net core 3.1 and the excellent channels.extensions library that makes it so much easier. I'd love to see a video on this.

  • @yuGesreveR
    @yuGesreveR Місяць тому +48

    OMG!!! I've been waiting for this feature for years!

    • @sinan720
      @sinan720 Місяць тому

      You could've just used System.Linq.Async.
      This functionality has already existed for years

  • @martinmanchev1276
    @martinmanchev1276 21 день тому +1

    The new feature looks good. There is an old way to achieve the same:
    var tasks = Enumerable.Range(1, 5).Select(Calculate).Select(async i => Console.WriteLine(await i));
    await Task.WhenAll(tasks);
    Here you reuse the defer idea of the LINQ

  • @Ilix42
    @Ilix42 Місяць тому +21

    Great video as always. One bit of feedback, please provide a link to referenced blog posts, especially when they're so old. Thanks!

  • @marvinjno-baptiste726
    @marvinjno-baptiste726 Місяць тому +31

    Been waiting for this for so long. Totally perplexed as to why it has taken so long for something so fundamental.

  • @devwatch2359
    @devwatch2359 Місяць тому

    Best segue into a course advertisement ever! So smart, so smooth... I applaud you.

  • @supreme_dev
    @supreme_dev Місяць тому +1

    Late happy birthday Nick! keep up with the great work

  • @C00l-Game-Dev
    @C00l-Game-Dev Місяць тому

    That is beautiful, thank you!

  • @tahaali01
    @tahaali01 Місяць тому

    So simple and clean, great!

  • @andersborum9267
    @andersborum9267 Місяць тому +4

    It's great to see an continuous investment in all areas of the framework. Would like to see more focus on reactive extensions (RX) though, especially around async APIs.

  • @JackBauerDev
    @JackBauerDev Місяць тому

    Wow that was way nicer than I expected

  • @timur-mut
    @timur-mut Місяць тому

    Great feature, thanks for explanation.

  • @tareksalha
    @tareksalha Місяць тому +2

    We are performing many of those independent tasks. Up until now, we have been using queues to solve the problem.

  • @orterves
    @orterves Місяць тому +55

    What happens when an exception is thrown by one (or more) of the tasks?

    • @vinydanylo
      @vinydanylo Місяць тому +10

      I think an AggregateException would be thrown.

    • @daravango
      @daravango Місяць тому +11

      My guess is an exception will be thrown when you `await` an individual task, and regarding the entire `await foreach` block, my guess would be: same behavior as `await`ing a `Task.WhenAll()`

    • @victor1882
      @victor1882 Місяць тому +3

      I would expect that you get the exception when awaiting, or check and don't await if the task is faulted

    • @Crozz22
      @Crozz22 Місяць тому +5

      Exception won't be thrown until you await the task. So it would not be thrown by Task.WhenEach.

    • @vothaison
      @vothaison Місяць тому

      Well, that's what people will explore for themselves.

  • @cemsayn9588
    @cemsayn9588 Місяць тому

    Thank you Nick for sharing this great feature. I have implemented my own as many of us for a batch System IO operation. I was checking in a While loop with task has completed, cancelled or hasException properties. Then removing from batch operation array and adding new one to task array. I don't know why Microsoft wait for this feature so far.

  • @danbopes
    @danbopes Місяць тому

    This is a nice addition to the task echo system for sure!

  • @dawizze1
    @dawizze1 Місяць тому +1

    I hope to see some F# content on Dometrain one day. Love to learn how to leverage the language to write apis.

  • @krccmsitp2884
    @krccmsitp2884 Місяць тому

    That looks neat!

  • @joepurdom2528
    @joepurdom2528 Місяць тому +11

    Couple questions
    1. When using WhenEach what thread does the Consle.WriteLine execute on?
    2. Is any overhead introduced by using await task vs task.Result, given the fact that we know task.Status == RanToCompletion? Or maybe we don't know that, depends on how errors are handled.
    3. Any difference in performance using WhenEach over await Task.WhenAll(tasks.Select(t => t.ContinueWith(async x => Console.WriteLine(await x)))); or is it just cleaner to look at?

    • @mohamedeffat54
      @mohamedeffat54 Місяць тому

      seconding question 3, I'd assume since nick didn't mention it that ContinueWith in general won't give the same intended behavior but I want to actually know.

  • @andrewallshouse4525
    @andrewallshouse4525 Місяць тому +5

    Hey Nick love this!! Can you post the link to the Stephen Toub article?

    • @nickchapsas
      @nickchapsas  Місяць тому +9

      devblogs.microsoft.com/pfxteam/processing-tasks-as-they-complete/

  • @xybersurfer
    @xybersurfer Місяць тому

    this is a really cool feature, that i didn't realize i needed

  • @oleksii766
    @oleksii766 Місяць тому

    As an option we can use a some kind of the Pub/Sub approach to subscribe to the results as they appear

  • @wojciechwilimowski985
    @wojciechwilimowski985 Місяць тому +1

    If you want to process tasks as they happen, you're two steps away from the point when it's serious enough to use System.Threading.Tasks.Dataflow

  • @AlFasGD
    @AlFasGD Місяць тому +60

    The video is very weirdly zoomed in some areas

    • @urzalukaskubicek9690
      @urzalukaskubicek9690 Місяць тому +53

      It's because the author is naked. He says it briefly right at the beginning of the video.

    • @AlFasGD
      @AlFasGD Місяць тому

      @@urzalukaskubicek9690 what the fuck are you saying

    • @felipe.raposo
      @felipe.raposo Місяць тому +2

      ​@@urzalukaskubicek9690😂😂😂

    • @sammtanX
      @sammtanX Місяць тому

      ​@@urzalukaskubicek9690that's crazy

    • @LC12345
      @LC12345 Місяць тому +3

      @@urzalukaskubicek9690Yes, he certainly does!

  • @amantinband
    @amantinband Місяць тому +4

    Thank god we have Stephen.
    Btw I think the following PLINQ does the same:
    await ParralelEnumerable.Range(1, 5)
    .ForAll(I => Comsole.WriteLine(Calculate(I)))

    • @nickchapsas
      @nickchapsas  Місяць тому +2

      I think the difference is that WhenEach will give you back the Task itself, not just the result so you have more flexibility on how you handle failure

    • @user-dc9zo7ek5j
      @user-dc9zo7ek5j Місяць тому +1

      @@nickchapsas I can't think of a use case where you want to continue the method without awaiting all tasks... additionally are 2 ways to do the thing you're describing already.
      1. Creating a method that consumes the original method's value and add there the custom logic.
      2. Use continuations.
      Here is a small program to demonstrate the second case:
      foreach (var t in Enumerable.Range(1, 6)
      .Select(async n => {
      await Task.Delay(new Random().Next(0, 10));
      return n;
      })
      .Select(t => t.ContinueWith(v => Console.WriteLine(v.Result))
      .ToArray())
      await t;

    • @petrx-ray9766
      @petrx-ray9766 Місяць тому

      @@user-dc9zo7ek5j Absolutely agree! I've never heard about the problem which Nick described, as it is easily handled by continuations.

    • @JohnWilliams-gy5yc
      @JohnWilliams-gy5yc 22 дні тому

      Not exactly the same under the hood. I guess "parallel" is named because it "must" be paralleled. With it, you want to request some OS threads to work on your tasks, not "limited" in only the running thread concurrency realm.

  • @timdoke
    @timdoke Місяць тому

    I've been using the exact approach you showed with WhenAny for quite a while. It's good there's an easier way, and more performant way to do it though.

  • @digibrett
    @digibrett Місяць тому

    This is awesome!

  • @thomassarmis
    @thomassarmis Місяць тому +1

    Why not appending a .ContinueWith() and then do the WhenAll() ? would the end result be the same?

  • @ricardoduarte442
    @ricardoduarte442 Місяць тому +5

    I would love to see a video of you looking at the implementation by microsoft and explaining why it is better :)
    Great vid as always Nick

  • @derangedftw
    @derangedftw Місяць тому

    Wow, finally!

  • @mome3807
    @mome3807 Місяць тому

    Does WhenEach run in parallel? hard to tell the difference to the serial syntax foreach task in listOfTasks { await task }

  • @7th_CAV_Trooper
    @7th_CAV_Trooper Місяць тому

    Why would you use a list instead of array in this sample?

    • @j1shin
      @j1shin Місяць тому +6

      Because of Remove()

    • @zpa89
      @zpa89 Місяць тому +1

      To expand on the other reply, it only works because it removes items from the list and you cannot remove items from an array. You would have to splice/copy the items to a new array. List works similarly behind the scenes but Microsoft is much better at optimization than you or I.

  • @timjackmaster1385
    @timjackmaster1385 Місяць тому +1

    Very very nice feature. Has anyone benchmarked how much more efficient it is in comparison to the old approach?

  • @haxi52
    @haxi52 Місяць тому

    Generally if I have a set of tasks I need chained, I'll wrap the call chain into another async method. So the caller really just needs to wait till everything is done.

  • @noellysaght1007
    @noellysaght1007 Місяць тому

    I achieved similar functionality using ActionBlocks from the TPL. I can see the benefits of this approach and how concise it is, but I think the functionality available in the TPL is under utilised when it comes to async processing.

  • @JustaFrogger
    @JustaFrogger Місяць тому

    Great!🎉❤

  • @kaiserbergin
    @kaiserbergin Місяць тому +1

    I'm trying to think of where I would use this... In examples like this, I would usually write a method or a class that orchestrates the order of events. The signature would return a task that encapsulates the order of events. I don't doubt this has valid use cases, I'm just curious to see how people plan on using it and if there's something I'm missing.

  • @handlez411
    @handlez411 Місяць тому

    Very very cool! 🥳

  • @johncerpa3782
    @johncerpa3782 Місяць тому

    Great video

  • @ThugLifeModafocah
    @ThugLifeModafocah Місяць тому

    that's really good indeed.

  • @realtimberstalker
    @realtimberstalker Місяць тому +9

    I feel like a simple solution to do this before .net 9 would have been to just wrap the task with another task that handles the result, and then awaitall those tasks instead.

    • @simonwood2448
      @simonwood2448 Місяць тому +6

      Indeed. I fail to see the excitement here, it's a trivial thing to solve

    • @realtimberstalker
      @realtimberstalker Місяць тому

      @@simonwood2448 This is syntactic sugar that is easier to use and reduces boilerplate. It’s certainly better. Im just saying I feel the original problem itself wasn’t some impossibly hard thing.

    • @protox4
      @protox4 Місяць тому +3

      That's not the same, though. That will process the results concurrently, while the new method processes sequentially.

    • @TazG2000
      @TazG2000 Місяць тому +1

      @@protox4 In the new method the tasks are still running concurrently, but the loop is responding to when each one completes. So this should, in theory, work the same way:
      await Task.WhenAll(
      tasks.Select(async t => {
      Console.WriteLine(await t);
      }));

    • @xybersurfer
      @xybersurfer Місяць тому

      @@TazG2000 that's still concurrent, because they are not sequentially writing to the console. each task writes as soon as it is finished, but they could be finished at the same time. you could probably make all tasks write sequentially with some kind of mutex like SemaphoreSlim, but that gets uglier

  • @gronkymug2590
    @gronkymug2590 Місяць тому

    I assume "await task" throws just for the specific task on exception, so we can nicely process errors of each task. I like it.

  • @montanomariano
    @montanomariano Місяць тому

    Awesome!

  • @oct8bit
    @oct8bit Місяць тому

    This nice one

  • @romanhrytskiv8845
    @romanhrytskiv8845 Місяць тому

    Very nice

  • @michaelsniknejs6326
    @michaelsniknejs6326 Місяць тому

    @3:43 "ton" although spelled with an "o" is actually pronounced "tun" (rhymes with "fun").

  • @tomtoups
    @tomtoups Місяць тому +1

    That's great if the T in Task[] are all the same. but if I'm calling multiple APIs using implicit parallelism, for example, and they all return a different types, then it doesn't really help me

    • @warrenbuckley3267
      @warrenbuckley3267 Місяць тому +4

      You could work around that by using Task. Then you could pattern match to figure out what model was returned.

    • @tomtoups
      @tomtoups Місяць тому

      @@warrenbuckley3267 Yeah that's an idea. 👍

    • @Cafe-O-Milk
      @Cafe-O-Milk Місяць тому

      @@warrenbuckley3267 very costly

  • @michaelakin766
    @michaelakin766 Місяць тому

    why do you have to do the await on the foreach and then on the task? It seems like the task is being called two times.

  • @sunefred
    @sunefred Місяць тому

    Very very elegant.

  • @d3tn3tracer
    @d3tn3tracer Місяць тому

    Finally! :)

  • @bgrant1512
    @bgrant1512 Місяць тому +1

    Wake me up when they make EF Core async friendly. That would make a huge difference.

  • @daddy2claire
    @daddy2claire Місяць тому

    Been using Stephen Toub's Interleaved method for the longest time. Nice to finally see this as a Task extension method.

  • @RoBBed13
    @RoBBed13 29 днів тому

    This is nice

  • @lordmetzgermeister
    @lordmetzgermeister Місяць тому

    I figured it would be solved by IAsyncEnumerable as that makes the most sense. Finally :)

  • @cdoubleplusgood
    @cdoubleplusgood Місяць тому +1

    "A sink a wait".
    I've seen it.

  • @kwibuske
    @kwibuske Місяць тому

    Is there a possibility to limit number of concurrent tasks it can await at once? Because if the input list is 1000 tasks long, spinning them all together will just create a bunch of overhead.

    • @Biker322
      @Biker322 Місяць тому

      Probably use a SemaphoreSlin

  • @JeppeRaskDK
    @JeppeRaskDK Місяць тому

    Looks cool, but couldn't you just move the "Calculate()" call to an async method which awaits and does the post-processing and then finally Task.WhenAll on those? I rarely have a List of tasks that I don't control the creation of.

  • @LogicException
    @LogicException Місяць тому

    Wild effect in this video

  • @xelesarc1680
    @xelesarc1680 Місяць тому

    Hi nick i need you help😢, i had some problem with parallel and async , i had 1000 batch data but need make it faster, i use parallel but got error in database why i cant crud if i use parallel , iam using sqlserver . If you read this maybe you can help me iam stuck 😢😢

  • @jesusdelarua5995
    @jesusdelarua5995 Місяць тому

    I do not see WhenEach() available in my .NET 9 Preview version.
    Getting Compiler Error CS0117 Task' does not contain a definition for 'WhenEach'
    My version is 9.0.100-preview.2.24157.14
    Ideas?

    • @xMadClawx
      @xMadClawx Місяць тому

      Same, I don’t even see it in the dotnet 9 preview on GitHub

  • @jorgepedraza1275
    @jorgepedraza1275 Місяць тому

    🤔 interesting!

  • @LordErnie
    @LordErnie Місяць тому

    Why not just use channels with separate sessions if you need resource syncing over single groups of tasks in a short process?

    • @pagorbunov
      @pagorbunov Місяць тому +1

      Because it would be overkill

  • @sinan720
    @sinan720 Місяць тому +2

    Why did'nt you mention System.Linq.Async?

  • @naftalyweinberger7892
    @naftalyweinberger7892 Місяць тому

    nick i am a huge fan of the dometrain courses, but I would prefer, that the presenters should make prepared slides rather then drawing while talking, it slows things down and i prefer when they talk FAST. deep dive C# is GGOOOOODDDDD

  • @jell0goeswiggle
    @jell0goeswiggle 27 днів тому

    Ive written that while loop before. Feels bad / looks ugly, but what are you going to do.
    I'm still enjoying great new things in .NET8, I dont need to be excited for 9 yet!

  • @xd-hood-classic
    @xd-hood-classic 9 днів тому

    Last time I had this problem, I just went with firing an event after task is done

  • @stunna4498
    @stunna4498 Місяць тому

    “Hello everybody my name is marioooo” sorry this was in my head when i heard the intro 😭😭😭😂

  • @ivanp_personal
    @ivanp_personal Місяць тому

    Nice feature. But I will actually have to wait for a next LTS version of .NET (likely 10) to use it in the real life.

  • @bluecup25
    @bluecup25 Місяць тому +1

    I just realized we're as far from 2012 as 2012 was from 2000

  • @CezaryWalenciuk
    @CezaryWalenciuk Місяць тому +1

    Ok what version of .NET 9 preview is this working. There is no "WhenEach" method

    • @Crezber
      @Crezber Місяць тому

      good question

  • @RealCheesyBread
    @RealCheesyBread Місяць тому +2

    Wait so what's the difference between `Task.WhenEach()` and `tasks.ToAsyncEnumerable()`?

    • @nanvlad
      @nanvlad Місяць тому

      WhenEach() is in BCL whereas ToAsyncEnumerable() is an reactive extension from System.Linq.Async

    • @RealCheesyBread
      @RealCheesyBread Місяць тому

      @@nanvlad But other than that, no difference?

    • @nanvlad
      @nanvlad Місяць тому

      @@RealCheesyBread BCL should be more reliable and performant. Also it can be improved by Microsoft on the very low level.

  • @gbjbaanb
    @gbjbaanb Місяць тому

    The only feature I've been waiting years for is to safely run these await async methods from a sync method. Damn mess Microsoft created.

  • @diegoronkkomaki6858
    @diegoronkkomaki6858 Місяць тому +2

    Not completely happy about the need to await the task inside the Task.WhenEach loop. If the WhenEach is supposed to yield a completed task why not access the result immediately in the loop, why the need to await the task "again"?
    EDIT: Is it because the tasks can return different types of results?

    • @scottbaldwin2477
      @scottbaldwin2477 Місяць тому +4

      You need it for error handling. This pattern lets you await a task in a try/catch, deal with any exceptions, and then continue iterating.

    • @jongeduard
      @jongeduard Місяць тому

      @scottbaldwin2477 Exactly, the place where the await is, there will happen the possible throws. Writing the entire thing myself while watching this video made that clear for me already.
      You can put a try block inside your await foreach block to handle each individual failed task. Which is the problem that you cannot with Task.WaitAll for example, which completely stops everything on the first exception.

    • @diegoronkkomaki6858
      @diegoronkkomaki6858 Місяць тому

      @@scottbaldwin2477 ah, of course. Makes sense.

    • @diegoronkkomaki6858
      @diegoronkkomaki6858 Місяць тому

      ​@@scottbaldwin2477 Right, makes sense.

  • @As_Ss
    @As_Ss Місяць тому

    Its taken so long as this was non issue, can be done in multiple ways.
    Normally u would limit the Tasks logical threads for example with max degree of parallel cuz u dont want to run more than that, so a new task wont start until there is place for another anyway and when that happens the ended task can report.
    This function is just a cherry on the ice cake.

  • @Grimlock1979
    @Grimlock1979 Місяць тому

    Yeah, this feature was long overdue.

  • @F1nalspace
    @F1nalspace Місяць тому

    I don't see any use-case for that feature. Just use a queue und do a while with await for each loop... you can even do a concurrent queue and do a parallel foreach... but still, what is the use-case for this? Process N-Items with the same type, waiting for each type until it its finished... thats like normal basic programming, not using any tasks at all... the point of tasks is do stuff in parallel - using the actual cores of a CPU, which people tend to forget that there is actual hardware running that code. Even in cases, where i do heavy data transformations and require multiple steps, even then i would not do it this way... maybe scrapping a website, parsing links... to prevent API blocking, due to too many request, would be the only use-case i could think of... Even multiple IO-access can still be done in parallel...

  • @andriiyustyk9378
    @andriiyustyk9378 Місяць тому +1

    5:40 Why "tasks.Any()" instead of "tasks.Count > 0"?

    • @zpa89
      @zpa89 Місяць тому

      Any seems a semantically easier to read. It is English not math. Plus no hard coded number. Technically slower, but modern computers can do billions to trillions of flops and the vast majority of all use cases will be bottlenecked by unmanaged resources like IO and network.

    • @keyser456
      @keyser456 Місяць тому

      @@zpa89 Are we after performance or more readable code? Gotta pick one.

    • @zpa89
      @zpa89 Місяць тому

      ​@@keyser456 you are neglecting that modern dotnet puts some SERIOUS effort into optimizing linq expressions. In fact, I would bet the compiler optimizes away the more basic extensions entirely when the underlying runtime type is known.

    • @keyser456
      @keyser456 Місяць тому

      @@zpa89 I'm neglecting nothing. There's an unhealthy obsession for "clean code", and it's to the point where people (by your own admission) are willing to trade off some performance. Will a tiny perf tradeoff be the deathblow in an app? Probably not. Is .Any() really that much better than Count > 0 from a readability standpoint? Definitely not. We're programming, not writing a book for kids.

    • @zpa89
      @zpa89 Місяць тому

      @@keyser456 we are all humans. We are writing code that humans must maintain. As humans, we speak in human languages. Writing code in a more naturally human way makes code easier to read.
      When a human wants to decide whether they have to deal with something, they ask "are there any x left?" They don't say "is the count of x greater than zero?"
      Beyond that, the vast majority of software engineers are dealing with IO/network bound work. CPU cycles you save by hyper optimizing your code pale in comparison to the time you must spend waiting for unmanaged requests to return.
      I would love to say that one day you will learn all of this but I have interviewed hundreds of engineers from all over the world, I lead a team of 60+, and unfortunately it seems that tenure just does not make you a more intelligent engineer. If you aren't one of the bright ones now, you may never be.

  • @weicco
    @weicco Місяць тому

    If people just used async methods and, what is even more important, would use CancellationToken so that api calls could actually be cancelled!

  • @MrBurikella
    @MrBurikella Місяць тому +3

    Well, I think the easiest way would be to get a list of tasks which includes processing. Like `Enumerable.Range(1, 5).Select(async order => Console.WriteLine(await Calculate(order)))`. Clearly you can replace `Console.WriteLine` by anything else. I think it is much cleaner and easy to grasp.

    • @protox4
      @protox4 Місяць тому +2

      It's not quite the same, though. That will process concurrently, while the new method processes sequentially.

    • @MrBurikella
      @MrBurikella Місяць тому

      @@protox4 right, that requirement wasn't stated explicitly

    • @zpa89
      @zpa89 Місяць тому

      ​@@MrBurikellathat is literally the entire point of the video....

  • @MEZOMEZO2011
    @MEZOMEZO2011 6 днів тому

    What is an actual use for this?

  • @the-avid-engineer
    @the-avid-engineer Місяць тому

    It seems odd that it returns an async enumerable of tasks, and not the results. I’m sure there’s a good reason for that but.. yeah.. odd.

  • @thedarkside0007
    @thedarkside0007 Місяць тому

    this just like select in golang

  • @ErazerPT
    @ErazerPT Місяць тому +4

    Maybe I'm just an old gezzer, but... if you want something done as they finish, why not pass them a callback? Yes, i know, context is gone, but if that is so paramount, is it not more of a "code smell" than anything else?

    • @milkandhenny
      @milkandhenny Місяць тому +1

      Callback hell, from nesting too many call backs is inevitable the moment a project becomes even the slight bit complex I'd assume.
      Being able to write I/O and other async tasks in a synchronous way just makes larger code bases more understandable for larger teams, or even yourself in the future

    • @petrusion2827
      @petrusion2827 Місяць тому

      You could've already done that by calling Task.ContinueWith(...) on all of the tasks in the list, if you want to deal with it and the problems that arise. C# was one of the first languages to use async await so it makes sense that the standard library is going to use it where it can.
      After all, "Callbacks are the goto statement of our generation"

    • @ErazerPT
      @ErazerPT Місяць тому

      @@milkandhenny How does nesting come into play? Something like OrderComplete(int order){...} being passed to something like Task ProcessOrder(int order,Action callback) was all they needed to finish their work, no nesting here, it's fully local. By the time WhenAll() resolves, you know they all went through OrderComplete() and move on.
      Think you're conflating "work completion callback" with "forward return point callback". The first finishes work, but the calling site is still waiting. The second IS the "forward return" point because the calling site IS NOT waiting.

    • @adambickford8720
      @adambickford8720 Місяць тому

      @@ErazerPT As long as none of your calls have dependencies, callbacks aren't terrible. As soon as you start having to coordinate them, callbacks are terrible.

    • @ErazerPT
      @ErazerPT Місяць тому

      @@adambickford8720 Once more, those are NOT the kind we are talking about.
      And yes, you're 100% right but Task's don't magically free you from it. If you're accessing shared resources, you'll still need some sort of await semaphore. Concurrency IS hell.

  • @luvincste
    @luvincste Місяць тому

    thought this was a video about async2

    • @protox4
      @protox4 Місяць тому

      He did a separate video about that. And that's just an experiment currently, not officially in 9.

  •  Місяць тому +3

    Or you could just have a function that does the Calculate(i) AND console.writeline ...
    WhenAll - that still does what it is suppose to do - waits for all of them.

    • @felipesfaria
      @felipesfaria Місяць тому

      This was my thought. It's what I would do in this situation.

  • @VandroiyIII
    @VandroiyIII Місяць тому +4

    Looks useful. Though I'm a bit torn about the ever increasing number of syntactic special cases in C#.
    The for, foreach, and now await foreach loops often take the place of higher-order functions, but proper higher-order functions are a bit crippled in their own special way.
    Doesn't this feel a bit like these projects where people only ever add features, but never refactor? A new programmer these days seems to be learning more and more cryptic rules for each real concept behind them.

    • @chris-pee
      @chris-pee Місяць тому

      To be fair, "await foreach" (IAsyncEnumerable) was added in 2019 in C# 8.

    • @C00l-Game-Dev
      @C00l-Game-Dev Місяць тому

      C# has always supported backwards compatibility when possible.

  • @ValueLevit
    @ValueLevit Місяць тому +7

    Am I missing something?
    tasksWithContinuation = taskList.Select(t => t.ContinueWith(delegate)).ToList();

    • @ricardoduarte442
      @ricardoduarte442 Місяць тому +1

      You are passing a delegate, won't resolve in the main thread, I think that is the difference

    • @ValueLevit
      @ValueLevit Місяць тому

      @@ricardoduarte442 if there's such a requirement then the needed behavior can be configured in the ContinueWith method.

    • @protox4
      @protox4 Місяць тому +1

      That will process the results concurrently, while WhenEach processes sequentially.

    • @ricardoduarte442
      @ricardoduarte442 Місяць тому

      @@protox4 Oh yeah true LOL

  • @gauravmanchanda4658
    @gauravmanchanda4658 Місяць тому +1

    It will be interesting to know what happens in race condition, if two tasks getting completed at same time, will this handle out of box or something that needs a special handling

    • @pagorbunov
      @pagorbunov Місяць тому

      Since the order is not guaranteed I don't think it's an issue at all

  • @Suriprofz
    @Suriprofz 27 днів тому

    Still feels hacky when used to go or rust channels

    • @Doggettxx
      @Doggettxx 19 днів тому

      c# has channels as well, not sure why he didn't talk about it since they're much more useful in most cases

  • @alexweekit
    @alexweekit Місяць тому

    Similar to JavaScript

  • @MrKulkoski
    @MrKulkoski Місяць тому

    This is great! But before instead of playing around with a list I would most probably just create a new method that would invoke both the original task and do the result processing and just run this one with Task.WhenAll. The list approach feels very engineery.

  • @LE8271
    @LE8271 Місяць тому

    Well Nick, after buying 12 of your courses you could have given me that one for free... :/

  • @Matlauk
    @Matlauk Місяць тому

    You said "no lists involved" when there is obviously a list involved. I understand enough that it could be any sort of IAsyncEnumerable but your code sample still used a list.

    • @ricardoduarte442
      @ricardoduarte442 Місяць тому +1

      I think is more about the allocation, is like using IEnumerable with ApplicationDbContext from EF, you are only bringing items to memory when u need them, in this case u are just getting the task pointers, not a list that have tasks (I AM NOT 100% SURE ON THIS, actually would like to see a response from Nick on this)

  • @bogdan.p
    @bogdan.p Місяць тому

    Something is wrong with your video as we can't see all the information. Seems like the encoding had some corrupted data.

  • @harcio
    @harcio Місяць тому

    What is the difference between this and Parallel.ForEachAsync?

  • @mvaddlu
    @mvaddlu Місяць тому +1

    Why dont just use callbacks? Seems like a problem out of nowhere

  • @AlexanderBelikov
    @AlexanderBelikov Місяць тому +1

    Huh, so many TaskHelper.WhenAll will be retired soon..

  • @eugenes9602
    @eugenes9602 Місяць тому

    Linq.Async definitely looks better than this

  • @Adiounys
    @Adiounys Місяць тому

    I don't understand what was actually hard in implenting this. The examples also makes no sense. Especially the 'while' loop - what is the purpose of this? Can't you just store 'WhenAll()' in a variable and check 'if(all.isCompleted) tasks.Clear()' after 'await WaitAny(task)'? I can also think of many other, better solutions to do the same thing. It seems you picked the worst possible implementation just for the contrast... PS. Sorry for my spellings I'm writing this on my phone.

    • @pagorbunov
      @pagorbunov Місяць тому

      That is why that new API was added. Instead of doing a common thing in thousand ways there would be official and most efficient one.