using System;
using System.Threading;
using System.Threading.Tasks;
namespace Dela.Mono.Examples
{
public class HelloWorld
{
private static int delayA = 3000;
private static int delayB = 3000;
private static void Log(string message)
{
Console.WriteLine("{0} {1}", DateTime.Now, message);
}
private static Task<int> LongCalcAsync(int x, int delay)
{
return Task.Factory.StartNew<int>(() => {
return LongCalc(x, delay);
});
}
private static int LongCalc(int x, int delay)
{
Log(String.Format("calc {0}", x));
Thread.Sleep(delay);
Log(String.Format("done {0}", x));
return x;
}
private static void Bar()
{
Log("bar");
}
// --------------------------------
private static async void Foo1()
{
var a = await LongCalcAsync(1, delayA);
var b = await LongCalcAsync(2, delayB);
Log(String.Format("foo {0} {1}", a, b));
}
private static async void Foo2()
{
var ca = LongCalcAsync(1, delayA);
var cb = LongCalcAsync(2, delayB);
var a = await ca;
var b = await cb;
Log(String.Format("foo {0} {1}", a, b));
}
private static async void Foo3a()
{
await Foo3b();
}
private static Task Foo3b()
{
return Task.Factory.StartNew(() => {
var a = LongCalc(1, delayA);
var b = LongCalc(2, delayB);
Log(String.Format("foo {0} {1}", a, b));
});
}
// --------------------------------
private static void Test1()
{
Foo1();
Bar();
Thread.Sleep(10000);
}
private static void Test2()
{
Foo2();
Bar();
Thread.Sleep(10000);
}
private static void Test3a()
{
Foo3a();
Bar();
Thread.Sleep(10000);
}
private static async void Test3b()
{
var done = Foo3b();
Bar();
await done;
}
// --------------------------------
public static void Main(string[] args)
{
Test1();
Console.WriteLine("--------");
Test2();
Console.WriteLine("--------");
Test3a();
Console.WriteLine("--------");
Test3b();
Thread.Sleep(10000);
}
}
}