fork download
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading.Tasks;
  4.  
  5. public class AsyncMethods {
  6. public async Task FirstMethodAsync() {
  7. Console.WriteLine("First async method started.");
  8. Stopwatch stopwatch = Stopwatch.StartNew();
  9. while (stopwatch.ElapsedMilliseconds < 1000) {}
  10. stopwatch.Stop();
  11. Console.WriteLine("First async method completed.");
  12. }
  13.  
  14. public async Task SecondMethodAsync() {
  15. Console.WriteLine("Second async method started.");
  16. Stopwatch stopwatch = Stopwatch.StartNew();
  17. while (stopwatch.ElapsedMilliseconds < 500) {}
  18. stopwatch.Stop();
  19. Console.WriteLine("Second async method completed.");
  20. }
  21. }
  22.  
  23. public class SyncMethods {
  24. public void FirstMethod() {
  25. Console.WriteLine("First sync method started.");
  26. Stopwatch stopwatch = Stopwatch.StartNew();
  27. while (stopwatch.ElapsedMilliseconds < 1000) {}
  28. stopwatch.Stop();
  29. Console.WriteLine("First sync method completed.");
  30. }
  31.  
  32. public void SecondMethod() {
  33. Console.WriteLine("Second sync method started.");
  34. Stopwatch stopwatch = Stopwatch.StartNew();
  35. while (stopwatch.ElapsedMilliseconds < 500) {}
  36. stopwatch.Stop();
  37. Console.WriteLine("Second sync method completed.");
  38. }
  39. }
  40.  
  41. public class Program {
  42. public static async Task Main() {
  43. AsyncMethods asyncMethods = new AsyncMethods();
  44. SyncMethods syncMethods = new SyncMethods();
  45.  
  46. syncMethods.FirstMethod();
  47. syncMethods.SecondMethod();
  48.  
  49. Task firstAsyncTask = asyncMethods.FirstMethodAsync();
  50. Task secondAsyncTask = asyncMethods.SecondMethodAsync();
  51.  
  52. await Task.WhenAll(firstAsyncTask, secondAsyncTask);
  53. Console.WriteLine("All methods have completed.");
  54. }
  55. }
Success #stdin #stdout 3.04s 30768KB
stdin
Standard input is empty
stdout
First sync method started.
First sync method completed.
Second sync method started.
Second sync method completed.
First async method started.
First async method completed.
Second async method started.
Second async method completed.
All methods have completed.