The `TaskSyncExtensions` class and its methods (`Sync` and `Sync<TResult>`) have been marked as `[System.Obsolete]`. These methods are no longer recommended due to the risk of deadlocks and unexpected behavior caused by synchronous blocking. Developers are advised to use `async/await` patterns instead. The warning messages indicate that these methods will be removed in a future release.
29 lines
1.5 KiB
C#
29 lines
1.5 KiB
C#
#if NETFRAMEWORK
|
|
using System.Threading.Tasks;
|
|
#endif
|
|
|
|
namespace ReC.Client
|
|
{
|
|
/// <summary>
|
|
/// Provides synchronous wrappers for Task-based operations.
|
|
/// </summary>
|
|
[System.Obsolete("Synchronous blocking helpers (Task.Sync) are no longer recommended. These methods can cause deadlocks or unexpected behavior. Rewrite calling code to use async/await (e.g. async Task tests). This helper class will be removed in a future release.", false)]
|
|
public static class TaskSyncExtensions
|
|
{
|
|
/// <summary>
|
|
/// Blocks until the task completes and propagates any exception.
|
|
/// </summary>
|
|
/// <param name="task">The task to wait for.</param>
|
|
[System.Obsolete("Use async/await instead of synchronous blocking. This method can cause deadlocks.", false)]
|
|
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>
|
|
[System.Obsolete("Use async/await instead of synchronous blocking. This method can cause deadlocks.", false)]
|
|
public static TResult Sync<TResult>(this Task<TResult> task) => task.ConfigureAwait(false).GetAwaiter().GetResult();
|
|
}
|
|
} |