#if NET20 || NET30 || NET35 || !NET_4_6 namespace System.Threading.Tasks { public partial class Task { public static Task Run(Action action) { var result = new Task(action, CancellationToken.None, TaskCreationOptions.DenyChildAttach); result.Start(); return result; } public static Task Run(Action action, CancellationToken cancellationToken) { var result = new Task(action, cancellationToken, TaskCreationOptions.DenyChildAttach); if (!cancellationToken.IsCancellationRequested) { result.Start(result.ExecutingTaskScheduler, false); } return result; } public static Task Run(Func action) { if (action == null) { throw new ArgumentNullException(); } var result = new Task ( () => { var task = action(); task.Start(task.ExecutingTaskScheduler, false); task.Wait(); if (task.IsFaulted) { throw task.Exception; } if (task.IsCanceled) { throw new TaskCanceledException(task); } }, CancellationToken.None, TaskCreationOptions.DenyChildAttach ); result.Start(); return result; } public static Task Run(Func action, CancellationToken cancellationToken) { if (action == null) { throw new ArgumentNullException(); } var result = new Task ( () => { var task = action(); task.Start(task.ExecutingTaskScheduler, false); task.Wait(cancellationToken); if (task.IsFaulted) { throw task.Exception; } if (task.IsCanceled) { throw new TaskCanceledException(task); } }, cancellationToken, TaskCreationOptions.DenyChildAttach ); if (!cancellationToken.IsCancellationRequested) { result.Start(result.ExecutingTaskScheduler, false); } return result; } public static Task Run(Func function) { var result = new Task(function, CancellationToken.None, TaskCreationOptions.DenyChildAttach); result.Start(); return result; } public static Task Run(Func function, CancellationToken cancellationToken) { var result = new Task(function, cancellationToken, TaskCreationOptions.DenyChildAttach); if (!cancellationToken.IsCancellationRequested) { result.Start(result.ExecutingTaskScheduler, false); } return result; } public static Task Run(Func> function) { if (function == null) { throw new ArgumentNullException(); } var result = new Task(() => function().Result, CancellationToken.None, TaskCreationOptions.DenyChildAttach); result.Start(); return result; } public static Task Run(Func> function, CancellationToken cancellationToken) { if (function == null) { throw new ArgumentNullException(); } var result = new Task(() => function().Result, cancellationToken, TaskCreationOptions.DenyChildAttach); if (!cancellationToken.IsCancellationRequested) { result.Start(result.ExecutingTaskScheduler, false); } return result; } } } #endif