Skip to content

Instantly share code, notes, and snippets.

@therealjohn
Forked from rid00z/AsyncHelper.cs
Created December 15, 2016 16:46
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 therealjohn/a47a2a8c421acbee606d055b41467610 to your computer and use it in GitHub Desktop.
Save therealjohn/a47a2a8c421acbee606d055b41467610 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.
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