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 async Task< int> AddAsync( int a, int b)
{
int result = await Task.Run ( ( ) => Add( a, b) ) ;
Console.WriteLine ( "Result computed = {0}" , result) ;
return result;
}
public static void Main( string[ ] args) {
int x = 30 ;
int y = 12 ;
Console.WriteLine ( "Main thread starts" ) ;
Task< int> task = AddAsync( x, y) ;
Console.WriteLine ( "Main thread waiting" ) ;
int sum = task.Result ;
Console.WriteLine ( "Main thread finishes, sum = {0}" , sum) ;
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uVGhyZWFkaW5nOwp1c2luZyBTeXN0ZW0uVGhyZWFkaW5nLlRhc2tzOwoKY2xhc3MgUHJvZ3JhbSB7CiAgcHVibGljIHN0YXRpYyBpbnQgQWRkKGludCBhLCBpbnQgYikgewogICAgIENvbnNvbGUuV3JpdGVMaW5lKCJDaGlsZCB0aHJlYWQgc3RhcnRzIik7CiAgICAgaW50IHJlc3VsdCA9IGEgKyBiOwogICAgIAogICAgIENvbnNvbGUuV3JpdGVMaW5lKCJDaGlsZCB0aHJlYWQgZ29lcyB0byBzbGVlcCIpOwogICAgIFRocmVhZC5TbGVlcCg1MDAwKTsgLy8gdGhlIHRocmVhZCBpcyBwYXVzZWQgZm9yIDUwMDAgbWlsbGlzZWNvbmRzCiAgICAgCiAgICAgQ29uc29sZS5Xcml0ZUxpbmUoIkNoaWxkIHRocmVhZCByZXN1bWVzIGFuZCBmaW5pc2hlcyIpOwogICAgIHJldHVybiByZXN1bHQ7CiAgfQogIAogIHB1YmxpYyBzdGF0aWMgYXN5bmMgVGFzazxpbnQ+IEFkZEFzeW5jKGludCBhLCBpbnQgYikKICB7CiAgICBpbnQgcmV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