Skip to content

Instantly share code, notes, and snippets.

@brainded
Created June 19, 2015 23:42
Show Gist options
  • Save brainded/cdc2760e9cc0dacd5971 to your computer and use it in GitHub Desktop.
Save brainded/cdc2760e9cc0dacd5971 to your computer and use it in GitHub Desktop.
Handy Extension class that allows Async execution synchronously.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace BobsBurgers
{
internal static class AsyncExtensions
{
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 AsyncExtensions._myTaskFactory
.StartNew<Task<TResult>>(func)
.Unwrap<TResult>()
.GetAwaiter()
.GetResult();
}
public static void RunSync(Func<Task> func)
{
AsyncExtensions._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