Skip to content

Instantly share code, notes, and snippets.

@kgiszewski
Created June 7, 2018 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kgiszewski/411e9a9b71e1c03c247bd9621c99e858 to your computer and use it in GitHub Desktop.
Save kgiszewski/411e9a9b71e1c03c247bd9621c99e858 to your computer and use it in GitHub Desktop.
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)
{
return TaskFactory
.StartNew<Task<TResult>>(func)
.Unwrap<TResult>()
.GetAwaiter()
.GetResult();
}
public static void RunSync(Func<Task> func)
{
TaskFactory
.StartNew<Task>(func)
.Unwrap()
.GetAwaiter()
.GetResult();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment