You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
downkyi/Brotli.Core/Implement/AsyncHelper.cs

68 lines
2.7 KiB

using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
namespace Brotli
{
internal class AsyncHelper
{
private static readonly TaskFactory _myTaskFactory = new
TaskFactory(CancellationToken.None, TaskCreationOptions.None,
TaskContinuationOptions.None, TaskScheduler.Default);
internal 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();
}
// Microsoft.AspNet.Identity.AsyncHelper
internal 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();
}
internal static TResult RunSync<TResult>(Func<object, Task<TResult>> func, object state, 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(state);
}, state).Unwrap<TResult>().ConfigureAwait(await).GetAwaiter().GetResult();
}
internal static TResult RunSync<TResult>(Func<object, Task<TResult>> func, object state, CancellationToken cancellationToken, 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(state);
}, state, cancellationToken).Unwrap<TResult>().ConfigureAwait(await).GetAwaiter().GetResult();
}
}
}