I’m currently learning about C#, I’m wondering how Thread, Task, async and await really work. This is what I understand:
- Task.Run(MyTask)is always executed on a new thread (in the thread pool)
- Because of this, Taskwill run in parallel with the method that calls it (because they are on two different threads)
- Therefore, use await to wait for the result of that Task, then continue executing the code below.
- This is my example code:
static void Main(string[] args)
{
    Task.Run(MyTask);
    Console.WriteLine("Do main work...");
    Thread.Sleep(5000);
}
static async Task MyTask()
{
    await MyMiniTask();
    Console.WriteLine("Do Task...");
}
static async Task MiniTask()
{
    await Task.Delay(1000);
    Console.WriteLine("Do Mini Task...");
}
output:
Do main work...
Do Mini Task...
Do Task...
As I understand it, the code should run like this:
- Run program -> Main thread is initialized to run the program
- Task.Run(MyTask)-> thread A in the thread pool will handle- MyTask
- In the output, print out “Do main work…” first, even though the console writeline is written after Task.Run(MyTask). The reason is because Main thread and thread A are two different threads, so they will be executed in parallel
- Back to thread A and MyTask, it encounters the statement: await MiniTask -> thread B in the thread pool will handleMiniTask(a new thread will always be used to handleMiniTaskeven if there is noawait)
- Because of using await,MyTaskwill have to wait forMiniTaskto complete, and while waiting, thead A of MyTask does not handle anything -> MyTask will be marked as "a pause point" here and C# will release thread A back to the thread pool, thread A can now do another job
- When MiniTaskis completed, C# will return to "the pause point" ofMyTask, and handle it with another thread in theThreadPool(it could be thread A or any other free thread)
 => The meaning of using async await is: it will wait for the result of the asynchronous task (MiniTask), await keyword when used will release the thread of the method that calls it (Thread A of MyTask), and ThreadPool will allocate another thread to continue processing this method after MiniTask has completed.
There is a difference between using the Wait() method of Task and the keyword await, which is Wait() will not release the thread of the method that calls it (I have tried)
=>> Summary (according to my understanding):
- When you want to perform multiple tasks in parallel without affecting the main thread -> use Thread, and now more modern isTask
- When you want to wait for the result of a Task->Wait()it
- When you want to wait for the result of a Task, but want to release the Thread of the method that calls thatTaskin the waiting time ->awaitit
I don't know if I understand that right?
 Sorry if my English is not good
This example code actually works and prints the output as above. But I don't know if it works the way I think or just a coincidence.
 
    