Skip to main content
deleted 62 characters in body
Source Link
Mathias Nohall
  • 757
  • 1
  • 9
  • 10

isnt the await statment making the code to run in sequential order? consider the following code

class Program { static Stopwatch _stopwatch = new(); static async Task Main(string[] args) { Console.WriteLine($"fire hot"); var carTask = BuyCar_stopwatch.Start(); var catTaskcarTask = FeedCatBuyCar(); var houseTaskcatTask = SellHouseFeedCat();   _stopwatch.Start();  var houseTask await= Task.WhenAllSellHouse(carTask, catTask, houseTask); await carTask; await catTask; await houseTask; Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} done!"); Console.WriteLine($"using await"); _stopwatch.Restart(); await BuyCar(); await FeedCat(); await SellHouse(); Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} done!"); } static async Task BuyCar() { Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} buy car started"); await Task.Delay(2000); Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} buy car done"); } static async Task FeedCat() { Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} feed cat started"); await Task.Delay(1000); Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} feed cat done"); } static async Task SellHouse() { Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} sell house started"); await Task.Delay(10); Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} sell house done"); } } fire hot 0 buy car started 03 feed cat started 04 sell house started 18 sell house done 1004 feed cat done 2013 buy car done 2014 done! using await 0 buy car started 2012 buy car done 2012 feed cat started 3018 feed cat done 3018 sell house started 3033 sell house done 3034 done! 

isnt the await statment making the code to run in sequential order? consider the following code

class Program { static Stopwatch _stopwatch = new(); static async Task Main(string[] args) { Console.WriteLine($"fire hot"); var carTask = BuyCar(); var catTask = FeedCat(); var houseTask = SellHouse();   _stopwatch.Start();  await Task.WhenAll(carTask, catTask, houseTask); await carTask; await catTask; await houseTask; Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} done!"); Console.WriteLine($"using await"); _stopwatch.Restart(); await BuyCar(); await FeedCat(); await SellHouse(); Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} done!"); } static async Task BuyCar() { Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} buy car started"); await Task.Delay(2000); Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} buy car done"); } static async Task FeedCat() { Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} feed cat started"); await Task.Delay(1000); Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} feed cat done"); } static async Task SellHouse() { Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} sell house started"); await Task.Delay(10); Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} sell house done"); } } fire hot 0 buy car started 0 feed cat started 0 sell house started 18 sell house done 1004 feed cat done 2013 buy car done 2014 done! using await 0 buy car started 2012 buy car done 2012 feed cat started 3018 feed cat done 3018 sell house started 3033 sell house done 3034 done! 

isnt the await statment making the code to run in sequential order? consider the following code

class Program { static Stopwatch _stopwatch = new(); static async Task Main(string[] args) { Console.WriteLine($"fire hot"); _stopwatch.Start(); var carTask = BuyCar(); var catTask = FeedCat(); var houseTask = SellHouse(); await carTask; await catTask; await houseTask; Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} done!"); Console.WriteLine($"using await"); _stopwatch.Restart(); await BuyCar(); await FeedCat(); await SellHouse(); Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} done!"); } static async Task BuyCar() { Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} buy car started"); await Task.Delay(2000); Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} buy car done"); } static async Task FeedCat() { Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} feed cat started"); await Task.Delay(1000); Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} feed cat done"); } static async Task SellHouse() { Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} sell house started"); await Task.Delay(10); Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} sell house done"); } } fire hot 0 buy car started 3 feed cat started 4 sell house started 18 sell house done 1004 feed cat done 2013 buy car done 2014 done! using await 0 buy car started 2012 buy car done 2012 feed cat started 3018 feed cat done 3018 sell house started 3033 sell house done 3034 done! 
Source Link
Mathias Nohall
  • 757
  • 1
  • 9
  • 10

isnt the await statment making the code to run in sequential order? consider the following code

class Program { static Stopwatch _stopwatch = new(); static async Task Main(string[] args) { Console.WriteLine($"fire hot"); var carTask = BuyCar(); var catTask = FeedCat(); var houseTask = SellHouse(); _stopwatch.Start(); await Task.WhenAll(carTask, catTask, houseTask); await carTask; await catTask; await houseTask; Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} done!"); Console.WriteLine($"using await"); _stopwatch.Restart(); await BuyCar(); await FeedCat(); await SellHouse(); Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} done!"); } static async Task BuyCar() { Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} buy car started"); await Task.Delay(2000); Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} buy car done"); } static async Task FeedCat() { Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} feed cat started"); await Task.Delay(1000); Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} feed cat done"); } static async Task SellHouse() { Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} sell house started"); await Task.Delay(10); Console.WriteLine($"{_stopwatch.ElapsedMilliseconds} sell house done"); } } fire hot 0 buy car started 0 feed cat started 0 sell house started 18 sell house done 1004 feed cat done 2013 buy car done 2014 done! using await 0 buy car started 2012 buy car done 2012 feed cat started 3018 feed cat done 3018 sell house started 3033 sell house done 3034 done!