I know this is old and there are a lot of articles and questions around however still it is confusing to understand, I have a simple basic example can someone explain it in a simplified way, basically I need to understand the flow.
I have two examples one with await and one without await, when I execute both, the output is identical, I believe I need to have 'Task.Run()' in order to execute asynchronously.
internal class Common
{
public async Task Process()
{
Console.WriteLine("Process Started");
DownloadileAsync();
Console.WriteLine("Process Completed");
}
public async Task DownloadileAsync()
{
DownloadFile();
Console.WriteLine("DownloadAsync");
}
public async Task DownloadFile()
{
Thread.Sleep(2000);
Console.WriteLine("File Downloaded");
}
}
=============================================================================
// one with await
internal class Common
{
public async Task Process()
{
Console.WriteLine("Process Started");
await DownloadileAsync();
Console.WriteLine("Process Completed");
}
public async Task DownloadileAsync()
{
await DownloadFile();
Console.WriteLine("DownloadAsync");
}
public async Task DownloadFile()
{
Thread.Sleep(2000);
Console.WriteLine("File Downloaded");
}
}
// Output
Main Thread Started
Process Started
File Downloaded
DownloadAsync
Process Completed
Main Thread Completed
The output is same for both approaches because you block the thread usign Thread.Sleep(2000)
in first approach. If you want it to be really async
replace it with await Task.Delay(2000)
and you will see the difference in output.
Using this:
public async Task Process()
{
Console.WriteLine("Process Started");
DownloadileAsync();
Console.WriteLine("Process Completed");
}
your just start the task and don't wait for it to be finished before executing the rest code after this task.
Imagine you have some async
method that returns result:
public async Task<int> GetResultAsync()
{
// instead of Thread.Sleep(2000); to avoid blocking
await Task.Delay(2000); // to simulate something like request to another API
return 10;
}
Having this method your approach without await
won't work here:
public async Task Process()
{
Console.WriteLine("Process Started");
var result = GetResultAsync();
Console.WriteLine("Process Completed");
// prints System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[System.Int32,Test.Enums.Program+<GetResultAsync>d__1]
// not 10
// because type of the result is Task<int>, not just int
Console.WriteLine($"Result is: {result}");
}
So here you don't wait the task to be finished. You just start the task and forget about it (fire-and-forget).
When you have await
:
public async Task Process()
{
Console.WriteLine("Process Started");
var result = await GetResultAsync(); // await here
Console.WriteLine("Process Completed");
// prints 10
Console.WriteLine($"Result is: {result}");
}
you asynchronously wait for task to be finished before continuation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.