Introduced TaskSyncExtensions class with Sync extension methods to allow synchronous execution of Task and Task<TResult> instances. This enables blocking on async code and is conditionally compiled for .NET Framework builds.
26 lines
991 B
C#
26 lines
991 B
C#
#if NETFRAMEWORK
|
|
using System.Threading.Tasks;
|
|
#endif
|
|
|
|
namespace ReC.Client
|
|
{
|
|
/// <summary>
|
|
/// Provides synchronous wrappers for Task-based operations.
|
|
/// </summary>
|
|
public static class TaskSyncExtensions
|
|
{
|
|
/// <summary>
|
|
/// Blocks until the task completes and propagates any exception.
|
|
/// </summary>
|
|
/// <param name="task">The task to wait for.</param>
|
|
public static void Sync(this Task task) => task.ConfigureAwait(false).GetAwaiter().GetResult();
|
|
|
|
/// <summary>
|
|
/// Blocks until the task completes and returns its result, propagating any exception.
|
|
/// </summary>
|
|
/// <typeparam name="TResult">The type of the task result.</typeparam>
|
|
/// <param name="task">The task to wait for.</param>
|
|
/// <returns>The result of the completed task.</returns>
|
|
public static TResult Sync<TResult>(this Task<TResult> task) => task.ConfigureAwait(false).GetAwaiter().GetResult();
|
|
}
|
|
} |