在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
public class AsyncHelper { private static readonly TaskFactory _myTaskFactory = new TaskFactory(CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Default); public static void RunSync(Func<Task> func, bool await = false) { CultureInfo cultureUi = CultureInfo.CurrentUICulture; CultureInfo culture = CultureInfo.CurrentCulture; _myTaskFactory.StartNew(delegate { Thread.CurrentThread.CurrentCulture = culture; Thread.CurrentThread.CurrentUICulture = cultureUi; return func(); }).Unwrap().ConfigureAwait(await).GetAwaiter().GetResult(); } public static TResult RunSync<TResult>(Func<Task<TResult>> func, bool await = false) { CultureInfo cultureUi = CultureInfo.CurrentUICulture; CultureInfo culture = CultureInfo.CurrentCulture; return _myTaskFactory.StartNew<Task<TResult>>(delegate { Thread.CurrentThread.CurrentCulture = culture; Thread.CurrentThread.CurrentUICulture = cultureUi; return func(); }).Unwrap<TResult>().ConfigureAwait(await).GetAwaiter().GetResult(); } }
应用
if (AsyncHelper.RunSync(() => _Service.Send(req), false))//_Service.Send(req) return bool { }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论