Guest User

Untitled

a guest
Apr 20th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. static class Program
  2. {
  3. static async Task<string> GetTaskAsync(int timeout)
  4. {
  5. Console.WriteLine("Task Thread: " + Thread.CurrentThread.ManagedThreadId);
  6. await Task.Delay(timeout);
  7. return timeout.ToString();
  8. }
  9.  
  10. static async Task Main()
  11. {
  12. Console.WriteLine("Main Thread: " + Thread.CurrentThread.ManagedThreadId);
  13.  
  14. Console.WriteLine("Should be greater than 5000");
  15. await Watch(NotParallel);
  16. Console.WriteLine("Should be less than 5000");
  17. await Watch(Parallel);
  18. }
  19.  
  20. public static async Task Parallel()
  21. {
  22. var res1 = GetTaskAsync(2000);
  23. var res2 = GetTaskAsync(3000);
  24.  
  25. Console.WriteLine("result: " + await res1 + await res2);
  26. }
  27.  
  28. public static async Task NotParallel()
  29. {
  30. var res1 = await GetTaskAsync(2000);
  31. var res2 = await GetTaskAsync(3000);
  32.  
  33. Console.WriteLine("result: " + res1 + res2);
  34. }
  35.  
  36. private static async Task Watch(Func<Task> func) {
  37. var sw = new Stopwatch();
  38. sw.Start();
  39.  
  40. await func?.Invoke();
  41.  
  42. sw.Stop();
  43. Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds);
  44. Console.WriteLine("---------------");
  45. }
  46. }
Add Comment
Please, Sign In to add comment