fork download
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4.  
  5. namespace Dela.Mono.Examples
  6. {
  7. public class HelloWorld
  8. {
  9. private static int delayA = 3000;
  10. private static int delayB = 3000;
  11.  
  12. private static void Log(string message)
  13. {
  14. Console.WriteLine("{0} {1}", DateTime.Now, message);
  15. }
  16.  
  17. private static Task<int> LongCalcAsync(int x, int delay)
  18. {
  19. return Task.Factory.StartNew<int>(() => {
  20. return LongCalc(x, delay);
  21. });
  22. }
  23.  
  24. private static int LongCalc(int x, int delay)
  25. {
  26. Log(String.Format("calc {0}", x));
  27. Thread.Sleep(delay);
  28. Log(String.Format("done {0}", x));
  29. return x;
  30. }
  31.  
  32. private static void Bar()
  33. {
  34. Log("bar");
  35. }
  36.  
  37. // --------------------------------
  38.  
  39.  
  40.  
  41. private static async void Foo1()
  42. {
  43. var a = await LongCalcAsync(1, delayA);
  44. var b = await LongCalcAsync(2, delayB);
  45. Log(String.Format("foo {0} {1}", a, b));
  46. }
  47.  
  48. private static async void Foo2()
  49. {
  50. var ca = LongCalcAsync(1, delayA);
  51. var cb = LongCalcAsync(2, delayB);
  52. var a = await ca;
  53. var b = await cb;
  54. Log(String.Format("foo {0} {1}", a, b));
  55. }
  56.  
  57. private static async void Foo3a()
  58. {
  59. await Foo3b();
  60. }
  61.  
  62. private static Task Foo3b()
  63. {
  64. return Task.Factory.StartNew(() => {
  65. var a = LongCalc(1, delayA);
  66. var b = LongCalc(2, delayB);
  67. Log(String.Format("foo {0} {1}", a, b));
  68. });
  69. }
  70.  
  71. // --------------------------------
  72.  
  73.  
  74.  
  75. private static void Test1()
  76. {
  77. Foo1();
  78. Bar();
  79. Thread.Sleep(10000);
  80. }
  81.  
  82. private static void Test2()
  83. {
  84. Foo2();
  85. Bar();
  86. Thread.Sleep(10000);
  87. }
  88.  
  89. private static void Test3a()
  90. {
  91. Foo3a();
  92. Bar();
  93. Thread.Sleep(10000);
  94. }
  95.  
  96. private static async void Test3b()
  97. {
  98. var done = Foo3b();
  99. Bar();
  100. await done;
  101. }
  102.  
  103. // --------------------------------
  104.  
  105.  
  106.  
  107. public static void Main(string[] args)
  108. {
  109. Test1();
  110. Console.WriteLine("--------");
  111. Test2();
  112. Console.WriteLine("--------");
  113. Test3a();
  114. Console.WriteLine("--------");
  115. Test3b();
  116. Thread.Sleep(10000);
  117. }
  118. }
  119. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty