🌵 Can Async/Await block the main thread?

Поделиться
HTML-код
  • Опубликовано: 31 июл 2024
  • In this video, I show how to use async/await pattern in the correct and prevent the main thread from being blocked.
    Check the repo: github.com/sa-es-ir/youtube-s...
    #asyncawait #csharp #dotnet #asynchronousprogramming #async
    Moments
    00:00 - Intro
    00:15 - async/await diagram
    02:16 - The code part of async/await
    04:58 - Async/Await blocks the main thread
    08:12 - The solutions for solving the async/await issue
    -------------------------------------------------------------------
    Follow me on LinkedIn:
    / sa-es-ir

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

  • @DotNetFun
    @DotNetFun 20 дней назад +1

    great video . Thanks Saeed!
    The Background services in ASP NET Core always cause confusion between developers.
    In terms of Background Services, it will act in a blocking fashion until reaching the first async method ( that is why some say that it is best practice to do a "await Task.Yield()" at the first line of your background jobs

    • @sa-es-ir
      @sa-es-ir  19 дней назад

      Well said, happy you like the video

  • @Fikusiklol
    @Fikusiklol 19 дней назад +1

    There's no guarantee that await returns the thread and runs on another.
    This code example is synchronous even tho it has multiple await keywords, thus code execution is continued using the same thread, until its blocked by that void method.
    If you want your program execution to be delayed - make it explicit, but inheriting from IHostedService interface instead of BackgroundService class.

    • @sa-es-ir
      @sa-es-ir  19 дней назад +1

      But using IHostedService won't solve the problem, we need to make sure to not use sync method in async methods. And it can happend a lot when using nuget packages that don't support async like Kafka consumer method!

    • @Fikusiklol
      @Fikusiklol 19 дней назад +1

      @@sa-es-ir It actually does solve it kind of. You might be doing database migrations, so it's fine to block startup.
      And sync methods are fine, as long as they are not I/O bound. Your whole workflow is synchronous here. Whole point of await keyword is make your operations non blocking for threads.
      Anyway, still a valid point. Thanks

    • @sa-es-ir
      @sa-es-ir  19 дней назад

      @@Fikusiklol For migrations totally agree as they are critical for the application and db, thanks for sharing your insight

  • @TellaTrix
    @TellaTrix 19 дней назад +1

    Very well spotted !

    • @sa-es-ir
      @sa-es-ir  19 дней назад

      Glad you like it

  • @umpgod7573
    @umpgod7573 20 дней назад

    why await ServiceMethod(or other await's for layers) not acting like Task.Yield? i dont really get it. I would expect it act like Task.Yield so other layers would run on another thread. But as you said its really running on same thread until db.

    • @sa-es-ir
      @sa-es-ir  20 дней назад

      async methods starting 'synchronously' and it makes sense.
      Task is an object and one of those methods should create an actual Task to return and who did that in my example? Yes Task.Delay!

  • @DotNetFun
    @DotNetFun 20 дней назад +1

    What do you mean by "main thread " ?ASP NET Core does not have main thread ( and synchronization context)

    • @sa-es-ir
      @sa-es-ir  19 дней назад +1

      You're right there is no synchronizationContext in the Asp.net core and the main thread is coming from my old habbits when was working with the winforms😁