public static class AsyncHelper { private static readonly TaskFactory _myTaskFactory = new TaskFactory(CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Default); /// <summary> /// 同步执行 /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="func"></param> /// <returns></returns> public static TResult RunSync<TResult>(Func<Task<TResult>> func) { return _myTaskFactory.StartNew(func).Unwrap().GetAwaiter().GetResult(); } /// <summary> /// /// </summary> /// <param name="func"></param> public static void RunSync(Func<Task> func) { _myTaskFactory.StartNew(func).Unwrap().GetAwaiter().GetResult(); } /// <summary> /// /// </summary> /// <param name="func"></param> public static void Run(Func<Task> func) { _myTaskFactory.StartNew(func); } /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="func"></param> public static void Run<T>(Func<Task<T>> func) { _myTaskFactory.StartNew(func); } }