zoukankan      html  css  js  c++  java
  • asynchelper在一个同步方法(非async)方法中等待async方法

        /// <summary>
        /// 异步调用助手类
        /// </summary>
        /// <see cref="http://www.tuicool.com/articles/QF73qaA"/>
        public static class AsyncHelper
        {
            public static void InvokeAndWait(Func<Task> asyncMethod)
            {
                // ReSharper disable once ConvertClosureToMethodGroup
                Task.Run(() => asyncMethod())
                    .ContinueWith(t => t.Wait())
                    .Wait();
            }
    
            public static T InvokeAndWait<T>(Func<Task<T>> asyncMethod)
            {
                var result = default(T);
                // ReSharper disable once ConvertClosureToMethodGroup
                Task.Run<T>(() => asyncMethod())
                    .ContinueWith(t =>
                    {
                        t.Wait();
                        result = t.Result;
                    }).Wait();
                return result;
            }
        }
        /// <summary>
        /// 异步调用助手类
        /// https://github.com/aspnet/AspNetIdentity.git
        /// </summary>
        public static class TaskAsyncHelper
        {
            private static readonly TaskFactory _myTaskFactory = new TaskFactory(CancellationToken.None,
                TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Default);
            /// <summary>
            /// demo: return TaskAsyncHelper.RunSync(() => manager.UpdateAsync(role));
            /// </summary>
            /// <typeparam name="TResult"></typeparam>
            /// <param name="func"></param>
            /// <returns></returns>
            public static TResult RunSync<TResult>(Func<Task<TResult>> func)
            {
                var cultureUi = CultureInfo.CurrentUICulture;
                var culture = CultureInfo.CurrentCulture;
                return _myTaskFactory.StartNew(() =>
                {
                    Thread.CurrentThread.CurrentCulture = culture;
                    Thread.CurrentThread.CurrentUICulture = cultureUi;
                    return func();
                }).Unwrap().GetAwaiter().GetResult();
            }
    
            /// <summary>
            /// demo: TaskAsyncHelper.RunSync(() => manager.SendEmailAsync(userId, subject, body));
            /// </summary>
            /// <param name="func"></param>
            public static void RunSync(Func<Task> func)
            {
                var cultureUi = CultureInfo.CurrentUICulture;
                var culture = CultureInfo.CurrentCulture;
                _myTaskFactory.StartNew(() =>
                {
                    Thread.CurrentThread.CurrentCulture = culture;
                    Thread.CurrentThread.CurrentUICulture = cultureUi;
                    return func();
                }).Unwrap().GetAwaiter().GetResult();
            }
        }

    此随笔或为自己所写、或为转载于网络。仅用于个人收集及备忘。

  • 相关阅读:
    Gatling的进阶二
    scala环境搭建
    web性能测试的新利器
    Jmeter+jenkins接口性能测试平台实践整理(二)
    Gatling的进阶一
    [经验总结]利用xlstproc处理XSLT的makefile
    VBA在WORD中给表格外的字体设置为标题
    VBA赋值给指定单元格
    将压缩包隐藏到图片中
    DB2删除重复数据
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/13330411.html
Copyright © 2011-2022 走看看