Created
June 19, 2015 23:42
-
-
Save brainded/cdc2760e9cc0dacd5971 to your computer and use it in GitHub Desktop.
Handy Extension class that allows Async execution synchronously.
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace BobsBurgers | |
{ | |
internal static class AsyncExtensions | |
{ | |
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 AsyncExtensions._myTaskFactory | |
.StartNew<Task<TResult>>(func) | |
.Unwrap<TResult>() | |
.GetAwaiter() | |
.GetResult(); | |
} | |
public static void RunSync(Func<Task> func) | |
{ | |
AsyncExtensions._myTaskFactory | |
.StartNew<Task>(func) | |
.Unwrap() | |
.GetAwaiter() | |
.GetResult(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment