首页 > 代码库 > 在同步中调用异步方法[.net 4.5]

在同步中调用异步方法[.net 4.5]

using System;using System.Threading;using System.Threading.Tasks;namespace Microsoft.AspNet.Identity{    internal static class AsyncHelper    {        private static readonly TaskFactory _myTaskFactory = new TaskFactory(CancellationToken.None,            TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Default);        public static TResult RunSync<TResult>(Func<Task<TResult>> func)        {            return _myTaskFactory.StartNew(func).Unwrap().GetAwaiter().GetResult();        }        public static void RunSync(Func<Task> func)        {            _myTaskFactory.StartNew(func).Unwrap().GetAwaiter().GetResult();        }    }}