Keep Your C# Application Smooth using Asynchronous Programming with Async/Await
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
Extremely useful one, thanks Nick. I would request similar content on the exception handling, Task cancellation in functions which are async returning Task.
Great suggestion!
Thanks for the excellent walkthrough!
My pleasure!
Easy pizzy. Thanks. I had forgotten the syntax
Fantastic stuff! Thank you!
Glad you enjoyed it!
good video with clear examples given. Thanks!
You are welcome!
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.
Excellent point 👍
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
Good idea. I'll get that on my list
well explained
Thanks for uploading this video. Could you please demonstrate the async await concept in a c# console application if possible.
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
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...
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. 😀
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!
@@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
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.
Could you make a video explaining these concepts using C++/CLI ?
Might look at this in future when I revisit c++ 👍
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);
}
}
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