Created
September 19, 2015 03:11
-
-
Save rid00z/96b875f096cbc7cbcba8 to your computer and use it in GitHub Desktop.
This code can be used to task a async task and run it without the async part.
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
public 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) | |
{ | |
return AsyncHelper._myTaskFactory | |
.StartNew<Task<TResult>>(func) | |
.Unwrap<TResult>() | |
.GetAwaiter() | |
.GetResult(); | |
} | |
public static void RunSync(Func<Task> func) | |
{ | |
AsyncHelper._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