Please look at this code:
class Program
{
static async void DoStuff()
{
StreamWriter s = new StreamWriter("a.txt");
WebClient wc = new WebClient();
await wc.DownloadStringTaskAsync(new Uri("http://www.microsoft.com")); //1
//await s.WriteAsync ("123"); //2
Thread.Sleep(10000);
Console.WriteLine("DoStuffEnd");
}
static void Main(string[] args)
{
Program.DoStuff();
Console.WriteLine("MainEnd");
Console.Read();
}
}
In this situation everything is ok, according to the logic of async/await.
Output:
MainEnd
After 10 seconds
DoStuffEnd
But if I comment (1) and uncomment (2), the output is:
After 10 seconds
DoStuffEnd
MainEnd
Why?