Skip to content

Instantly share code, notes, and snippets.

@SaahilClaypool
Created June 26, 2022 23:07
Show Gist options
  • Save SaahilClaypool/b3f3884b039ce39ca4fd1f52a6f6750d to your computer and use it in GitHub Desktop.
Save SaahilClaypool/b3f3884b039ce39ca4fd1f52a6f6750d to your computer and use it in GitHub Desktop.
Sync over Async Helper
// Copyright (c) Microsoft Corporation, Inc. All rights reserved.
// Copyright (c) Saahil Claypool
// Licensed under the MIT License
namespace SyncOverAsync;
public static class AsyncHelper
{
private static readonly TaskFactory _myTaskFactory =
new(CancellationToken.None,
TaskCreationOptions.None,
TaskContinuationOptions.None,
TaskScheduler.Default);
public static TResult Block<TResult>(this Func<Task<TResult>> func)
{
return _myTaskFactory
.StartNew(func)
.Unwrap()
.GetAwaiter()
.GetResult();
}
public static void Block(this Func<Task> func)
{
_myTaskFactory
.StartNew(func)
.Unwrap()
.GetAwaiter()
.GetResult();
}
public static void Block(this Task task) => Block(() => task);
public static TResult Block<TResult>(this Task<TResult> task) => Block(() => task);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment