Created
December 13, 2018 03:07
-
-
Save Anduin2017/ac8a62e73f59a68ad27fb8f1c0e71f25 to your computer and use it in GitHub Desktop.
Run async method sync in C#
This file contains hidden or 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 _taskFactory = new | |
TaskFactory(CancellationToken.None, | |
TaskCreationOptions.None, | |
TaskContinuationOptions.None, | |
TaskScheduler.Default); | |
public static TResult RunSync<TResult>(Func<Task<TResult>> func) | |
=> _taskFactory | |
.StartNew(func) | |
.Unwrap() | |
.GetAwaiter() | |
.GetResult(); | |
public static void RunSync(Func<Task> func) | |
=> _taskFactory | |
.StartNew(func) | |
.Unwrap() | |
.GetAwaiter() | |
.GetResult(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment