Keep Your C# Application Smooth using Asynchronous Programming with Async/Await

Поделиться
HTML-код
  • Опубликовано: 28 авг 2024
  • Asynchronous programming at its most basic level is a way of executing a potentially long running task but doing so in a way that doesn't block the whole program and makes it so that the program is still responsive to events and user input.
    In this video, I'll show you how to write asynchronous code in C# using the async and await keywords. I'll use an example of a C# WinForms application that calls a slow API, demonstrating how you can allow the API call to run while still being able to interact with the UI simultaneously.
    #dotnet #csharp #code #softwaredevelopment #softwareengineer #asynchronous

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

  • @manojambhore2772
    @manojambhore2772 Год назад +6

    Extremely useful one, thanks Nick. I would request similar content on the exception handling, Task cancellation in functions which are async returning Task.

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

    I appreciate the clear spoken English lol. You never know these days in the programming world.
    The button call returns a void, not Task, therefore it cannot be awaited. Is that good practice? May also be not handy during tests or bug hunting.

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

    Very useful video. Have you considered creating complete tutorials on C# and other MS-related tech? Maybe create a project of ASP MVC with React, etc. how about that? cheers

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

    Fantastic stuff! Thank you!

  • @user-pq4gu9gc8w
    @user-pq4gu9gc8w 6 месяцев назад

    Easy pizzy. Thanks. I had forgotten the syntax

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

    Thanks for the excellent walkthrough!

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

    good video with clear examples given. Thanks!

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

    Thanks for uploading this video. Could you please demonstrate the async await concept in a c# console application if possible.

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

      Concept would be basically the same. You use the async keyword in the signature of any method or function you wish to make asynchronous, and then you can await the code from another async method or function

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

    well explained

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

    Could you make a video explaining these concepts using C++/CLI ?

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

      Might look at this in future when I revisit c++ 👍

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

      I quickly made something for you:
      ***************************************************************************************************
      Per MS Documentation: The await operator suspends evaluation of
      the enclosing async method until the asynchronous operation represented by its operand completes.
      The await operator doesn't block the thread that evaluates the async method. When the await operator
      suspends the enclosing async method, the control returns to the caller of the method.
      ***************************************************************************************************
      */
      internal class Program
      {
      public static async Task DoStuffAsync()
      {
      for (int i = 1; i < 6; i++)
      Console.WriteLine(i);
      await Task.Delay(2000);
      return "Done: DoStuffAsync";
      }
      static async Task Main(string[] args)
      {
      Task DoStuffTask = DoStuffAsync();
      Console.WriteLine("Hehe, lemme sneak in here :)");
      await DoStuffTask;
      Console.WriteLine(DoStuffTask.Result);
      }
      }

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

    Thank you! What is the purpose of surrounding your http call with a using statement? I've seen this kind of coding before but not really sure why it's used...

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

      Usually, the using in the manner is to provide automatic disposal of the object that is created. As Nick says here, the purists might argue about its use here, but the idea of disposal of objects is to avoid memory leaks. 😀

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

      It closes the connection (security issue) in this case and marks used resources for cleanup (the system decides when to do that exactly).
      Using this is a good habit and often indispensable in serious code. Is also used, for example when addressing databases. Very important and good habit!

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

      @@angel_macharielMuch appreciated, I was starting to code back then and now I 've seen it more, do you know any youtube vide that goes through this topic more in depth? Thanks again

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

      The using statement is nicely explained on the Microsoft website "using statement - ensure the correct use of disposable objects".'
      It's not some rocket science to solve, it's more about getting used to it.

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

    Could u do a video explaining the concepts of parameters in API, I mean with restsharp, how to insert headers, body, authorization and comparing that to postman