Add TaskSyncExtensions for sync Task execution (.NET FW)
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.
This commit is contained in:
26
src/ReC.Client/TaskSyncExtensions.cs
Normal file
26
src/ReC.Client/TaskSyncExtensions.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
#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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user