using System;
using System.Threading;
using System.Threading.Tasks;
public class Program
{
public static void Main()
var fooTask =
Task.Factory.StartNew(() =>
Thread.Sleep(50);
return 42;
});
if (!Task.WaitAll(new []{fooTask}, TimeSpan.FromMilliseconds(300)))
throw new TaskCanceledException(fooTask);
}
Console.WriteLine(fooTask.Result);