Created
June 26, 2022 23:07
-
-
Save SaahilClaypool/b3f3884b039ce39ca4fd1f52a6f6750d to your computer and use it in GitHub Desktop.
Sync over Async Helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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