using System;
using System.Threading;
using System.Threading.Tasks;
class Program {
public static int Add(int a, int b) {
Console.WriteLine("Child thread starts");
int result = a + b;
Console.WriteLine("Child thread goes to sleep");
Thread.Sleep(5000); // the thread is paused for 5000 milliseconds
Console.WriteLine("Child thread resumes and finishes");
return result;
}
public static void Main(string[] args) {
int x = 30;
int y = 12;
Task<int> task = new Task<int>(() => Add(x, y));
Console.WriteLine("Main thread starts");
task.Start();
Console.WriteLine("Main thread waiting");
int sum = task.Result;
Console.WriteLine("Main thread finishes, sum = {0}", sum);
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uVGhyZWFkaW5nOwp1c2luZyBTeXN0ZW0uVGhyZWFkaW5nLlRhc2tzOwoKY2xhc3MgUHJvZ3JhbSB7CiAgcHVibGljIHN0YXRpYyBpbnQgQWRkKGludCBhLCBpbnQgYikgewogICAgIENvbnNvbGUuV3JpdGVMaW5lKCJDaGlsZCB0aHJlYWQgc3RhcnRzIik7CiAgICAgaW50IHJlc3VsdCA9IGEgKyBiOwogICAgIAogICAgIENvbnNvbGUuV3JpdGVMaW5lKCJDaGlsZCB0aHJlYWQgZ29lcyB0byBzbGVlcCIpOwogICAgIFRocmVhZC5TbGVlcCg1MDAwKTsgLy8gdGhlIHRocmVhZCBpcyBwYXVzZWQgZm9yIDUwMDAgbWlsbGlzZWNvbmRzCiAgICAgCiAgICAgQ29uc29sZS5Xcml0ZUxpbmUoIkNoaWxkIHRocmVhZCByZXN1bWVzIGFuZCBmaW5pc2hlcyIpOwogICAgIHJldHVybiByZXN1bHQ7CiAgfQogIAogIHB1YmxpYyBzdGF0aWMgdm9pZCBNYWluKHN0cmluZ1tdIGFyZ3MpIHsKICAgICBpbnQgeCA9IDMwOwogICAgIGludCB5ID0gMTI7CiAgICAgCiAgICAgVGFzazxpbnQ+IHRhc2sgPSBuZXcgVGFzazxpbnQ+KCgpID0+IEFkZCh4LCB5KSk7CiAgICAgQ29uc29sZS5Xcml0ZUxpbmUoIk1haW4gdGhyZWFkIHN0YXJ0cyIpOwogICAgIHRhc2suU3RhcnQoKTsKCiAgICAgQ29uc29sZS5Xcml0ZUxpbmUoIk1haW4gdGhyZWFkIHdhaXRpbmciKTsKICAgICBpbnQgc3VtID0gdGFzay5SZXN1bHQ7CiAgICAgQ29uc29sZS5Xcml0ZUxpbmUoIk1haW4gdGhyZWFkIGZpbmlzaGVzLCBzdW0gPSB7MH0iLCBzdW0pOwogIH0KfQ==