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