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

Sdílet
Vložit
  • čas přidán 3. 01. 2023
  • 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
  • Věda a technologie

Komentáře • 25

  • @manojambhore2772
    @manojambhore2772 Před rokem +6

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

  • @neilranada
    @neilranada Před 10 měsíci

    Thanks for the excellent walkthrough!

  • @user-pq4gu9gc8w
    @user-pq4gu9gc8w Před 4 měsíci

    Easy pizzy. Thanks. I had forgotten the syntax

  • @jmischa
    @jmischa Před rokem +1

    Fantastic stuff! Thank you!

  • @AllAboutDataTechnology
    @AllAboutDataTechnology Před 6 měsíci

    good video with clear examples given. Thanks!

  • @angel_machariel
    @angel_machariel Před 11 měsíci +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.

  • @higherpurpose1212
    @higherpurpose1212 Před rokem +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

  • @scotolivera8207
    @scotolivera8207 Před rokem +1

    well explained

  • @deanambrox8069
    @deanambrox8069 Před rokem +2

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

    • @nickproudprogrammer
      @nickproudprogrammer  Před rokem

      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

  • @tamboleo
    @tamboleo Před rokem

    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 Před rokem +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 Před 11 měsíci +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 Před 11 měsíci

      @@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 Před 11 měsíci

      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.

  • @adarshpanda6062
    @adarshpanda6062 Před rokem +1

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

    • @nickproudprogrammer
      @nickproudprogrammer  Před rokem

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

    • @angel_machariel
      @angel_machariel Před 11 měsíci

      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);
      }
      }

  • @miguelperezs3022
    @miguelperezs3022 Před rokem +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