Created
March 12, 2019 03:12
-
-
Save chenbojian/1c0b4141900354ead711d2517fa3ec93 to your computer and use it in GitHub Desktop.
AsyncHelper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/aspnet/AspNetIdentity/blob/master/src/Microsoft.AspNet.Identity.Core/AsyncHelper.cs | |
// Copyright (c) Microsoft Corporation, Inc. All rights reserved. | |
// Licensed under the MIT License, Version 2.0. See License.txt in the project root for license information. | |
using System; | |
using System.Globalization; | |
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) | |
{ | |
var cultureUi = CultureInfo.CurrentUICulture; | |
var culture = CultureInfo.CurrentCulture; | |
return _myTaskFactory.StartNew(() => | |
{ | |
Thread.CurrentThread.CurrentCulture = culture; | |
Thread.CurrentThread.CurrentUICulture = cultureUi; | |
return func(); | |
}).Unwrap().GetAwaiter().GetResult(); | |
} | |
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment