Skip to content

Instantly share code, notes, and snippets.

@rid00z
Created September 19, 2015 03:11
Show Gist options
  • Save rid00z/96b875f096cbc7cbcba8 to your computer and use it in GitHub Desktop.
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.
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