Asynchronous programming is not something that can be explained in comments. You should get out and read as many articles as possible. Be forewarned, though, that many tutorials out there have the mindset that asynchronous programming is about multi threading. It is NOT. Multi threading is just one form of asynchronous programming.
Imagine you are cooking mashed potatoes. Let's define the process in 5 tasks:
Dice potatoes.
Cook the diced potatoes.
Grate cheese.
Mash potatoes.
Mix the mashed potatoes with the grated cheese.
You will do this alone. You are the thread.
You could go through the list of tasks and do this list sequentially, one task after the other. You noticed, however, that you were doing nothing while the potatoes were being cooked. You could have grated the cheese while the potatoes were cooking. So you repeat the process, this time not waiting for the potatoes to cook. Clearly this is an improvement. Now you (the thread) are able to finish faster. You have made better use of the CPU time.
In code:
DicePotatoes();// Synchronous task.varcookTask=CookPotatoes();// Asynchronous task. You start it, but you don't await it.GrateCheese();// Synchronous task.// Now you are ready to mash, then mix.// Now you await.awaitcookTask;// Now you complete the recipe.MashPotatoes();// Synchronous task.Mix();// Synchronous task.
So the lesson here is twofold:
We did asynchronous programming, and we did not use another thread. We did single-threaded asynchronous programming.
You only await when you absolutely have to. Do not immediately and blindly await any async function. The await keyword synchronizes. Only use it when you need to synchronize.
Not sure I follow. Are you asking a question? I'll try to guess.
With an asynchronous function, you have 2 choices: Await it, or not await it.
If you await it, the executing thread will immediately mark as "do for later" all of the code below the await and will go look for work to do to its task queue.
If you don't await it, the asynchronous task is started, but the executing thread that started it does not wait for it to finish, and continues processing the code. This is the case I explained. Cooking is started but not awaited. The thread continued to grate the cheese while the kitchen cooktop cooked the potatoes.
Either choice is valid as long as it gets you the desired result, ideally in the fastest possible time.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Asynchronous programming is not something that can be explained in comments. You should get out and read as many articles as possible. Be forewarned, though, that many tutorials out there have the mindset that asynchronous programming is about multi threading. It is NOT. Multi threading is just one form of asynchronous programming.
Imagine you are cooking mashed potatoes. Let's define the process in 5 tasks:
You will do this alone. You are the thread.
You could go through the list of tasks and do this list sequentially, one task after the other. You noticed, however, that you were doing nothing while the potatoes were being cooked. You could have grated the cheese while the potatoes were cooking. So you repeat the process, this time not waiting for the potatoes to cook. Clearly this is an improvement. Now you (the thread) are able to finish faster. You have made better use of the CPU time.
In code:
So the lesson here is twofold:
awaitkeyword synchronizes. Only use it when you need to synchronize.Thank you very much, that's mean , I only call the async function (it will be async) and assign it to variable
var cookTask = CookPotatoes();
Not sure I follow. Are you asking a question? I'll try to guess.
With an asynchronous function, you have 2 choices: Await it, or not await it.
If you await it, the executing thread will immediately mark as "do for later" all of the code below the
awaitand will go look for work to do to its task queue.If you don't await it, the asynchronous task is started, but the executing thread that started it does not wait for it to finish, and continues processing the code. This is the case I explained. Cooking is started but not awaited. The thread continued to grate the cheese while the kitchen cooktop cooked the potatoes.
Either choice is valid as long as it gets you the desired result, ideally in the fastest possible time.