Skip to content

Instantly share code, notes, and snippets.

@Anduin2017
Created December 13, 2018 03:07
Show Gist options
  • Save Anduin2017/ac8a62e73f59a68ad27fb8f1c0e71f25 to your computer and use it in GitHub Desktop.
Save Anduin2017/ac8a62e73f59a68ad27fb8f1c0e71f25 to your computer and use it in GitHub Desktop.
Run async method sync in C#
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