using System; using System.Threading; using System.Threading.Tasks; namespace ZeroLevel.Services.Async { public static class AsyncHelper { private static readonly TaskFactory _taskFactory = new TaskFactory(CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Default); public static TResult RunSync(Func> func) => _taskFactory .StartNew(func) .Unwrap() .GetAwaiter() .GetResult(); public static void RunSync(Func func) => _taskFactory .StartNew(func) .Unwrap() .GetAwaiter() .GetResult(); public async static Task WithTimeout(this Task task, int duration) { var retTask = await Task.WhenAny(task, Task.Delay(duration)) .ConfigureAwait(false); if (retTask is Task) return task.Result; return default(T); } } }