You can create a new Task using Task.Delay and use Task.WhenAny :
Task delayedTask = Task.Delay(TimeSpan.FromMinutes(5)); Task workerTask = Task.Factory.StartNew(() => { Aspose.Words.Document doc = new Aspose.Words.Document(inputFileName); doc.Save(Path.ChangeExtension(inputFileName, ".pdf")); }); if (await Task.WhenAny(delayedTask, workerTask) == delayedTask) {
You can use Microsoft.Bcl.Async to add async-await features to .NET 4.0
Edit:
As you are using VS2010, you can use Task.Factory.ContinueWheAny instead:
Task.Factory.ContinueWhenAny(new[] { delayedTask, workerTask }, task => { if (task == delayedTask) {
Edit 2:
Since Task.Delay not available in .NET 4.0, you can create it yourself using the extension method:
public static class TaskExtensions { public static Task Delay(this Task task, TimeSpan timeSpan) { var tcs = new TaskCompletionSource<bool>(); System.Timers.Timer timer = new System.Timers.Timer(); timer.Elapsed += (obj, args) => { tcs.TrySetResult(true); }; timer.Interval = timeSpan.Milliseconds; timer.AutoReset = false; timer.Start(); return tcs.Task; } }
source share