Guest User

Untitled

a guest
Mar 13th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading.Tasks;
  4.  
  5. namespace dotnetasync
  6. {
  7. public static class Delayer
  8. {
  9. public static async Task<int> Get(int id)
  10. {
  11. Console.WriteLine($"Starting task nr {id}");
  12.  
  13. await Task.Delay(id);
  14.  
  15. Console.WriteLine($"Completed task nr {id}");
  16.  
  17. return id;
  18. }
  19. }
  20.  
  21. public class InParallel
  22. {
  23. public static async Task RunFunStuff()
  24. {
  25. var sw = new Stopwatch();
  26. sw.Start();
  27. var aTask = Delayer.Get(2000);
  28. var bTask = Delayer.Get(1000);
  29. var cTask = Delayer.Get(5);
  30.  
  31. // this is really optional here - it doesn't change the result since we're still read value from each of the tasks
  32. await Task.WhenAll(aTask, bTask, cTask);
  33.  
  34. var a = await aTask;
  35. var b = await bTask;
  36. var c = await cTask;
  37. sw.Stop();
  38.  
  39. Console.WriteLine($"Async completed - got {a} {b} {c} in {sw.ElapsedMilliseconds}\n\t");
  40. }
  41. }
  42. }
Add Comment
Please, Sign In to add comment