155 lines
6.1 KiB
C#
155 lines
6.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Runtime.ExceptionServices;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ShardingCore.Extensions
|
|
{
|
|
/*
|
|
* @Author: xjm
|
|
* @Description:
|
|
* @Date: 2021/9/1 10:22:00
|
|
* @Ver: 1.0
|
|
* @Email: 326308290@qq.com
|
|
*/
|
|
[ExcludeFromCodeCoverage]
|
|
internal static class TaskExtension
|
|
{
|
|
/// <summary>
|
|
/// 是否成功
|
|
/// </summary>
|
|
/// <param name="task"></param>
|
|
/// <returns></returns>
|
|
public static bool IsCompletedSuccessfully(this Task task)
|
|
{
|
|
return task.IsCompleted && !(task.IsCanceled || task.IsFaulted);
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Waits for the task to complete, unwrapping any exceptions.
|
|
/// </summary>
|
|
/// <param name="task">The task. May not be <c>null</c>.</param>
|
|
public static void WaitAndUnwrapException(this Task task)
|
|
{
|
|
if (task == null)
|
|
throw new ArgumentNullException(nameof(task));
|
|
task.GetAwaiter().GetResult();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Waits for the task to complete, unwrapping any exceptions.
|
|
/// </summary>
|
|
/// <param name="task">The task. May not be <c>null</c>.</param>
|
|
/// <param name="cancellationToken">A cancellation token to observe while waiting for the task to complete.</param>
|
|
/// <exception cref="OperationCanceledException">The <paramref name="cancellationToken"/> was cancelled before the <paramref name="task"/> completed, or the <paramref name="task"/> raised an <see cref="OperationCanceledException"/>.</exception>
|
|
public static void WaitAndUnwrapException(this Task task, CancellationToken cancellationToken)
|
|
{
|
|
if (task == null)
|
|
throw new ArgumentNullException(nameof(task));
|
|
try
|
|
{
|
|
task.Wait(cancellationToken);
|
|
}
|
|
catch (AggregateException ex)
|
|
{
|
|
ExceptionDispatchInfo.Capture(ex).Throw();
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Waits for the task to complete, unwrapping any exceptions.
|
|
/// </summary>
|
|
/// <typeparam name="TResult">The type of the result of the task.</typeparam>
|
|
/// <param name="task">The task. May not be <c>null</c>.</param>
|
|
/// <returns>The result of the task.</returns>
|
|
public static TResult WaitAndUnwrapException<TResult>(this Task<TResult> task)
|
|
{
|
|
if (task == null)
|
|
throw new ArgumentNullException(nameof(task));
|
|
return task.GetAwaiter().GetResult();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Waits for the task to complete, unwrapping any exceptions.
|
|
/// </summary>
|
|
/// <typeparam name="TResult"></typeparam>
|
|
/// <param name="task"></param>
|
|
/// <param name="continueOnCapturedContext"></param>
|
|
/// <returns>The result of the task.</returns>
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
public static TResult WaitAndUnwrapException<TResult>(this Task<TResult> task,bool continueOnCapturedContext)
|
|
{
|
|
if (task == null)
|
|
throw new ArgumentNullException(nameof(task));
|
|
return task.ConfigureAwait(continueOnCapturedContext).GetAwaiter().GetResult();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Waits for the task to complete, unwrapping any exceptions.
|
|
/// </summary>
|
|
/// <typeparam name="TResult">The type of the result of the task.</typeparam>
|
|
/// <param name="task">The task. May not be <c>null</c>.</param>
|
|
/// <param name="cancellationToken">A cancellation token to observe while waiting for the task to complete.</param>
|
|
/// <returns>The result of the task.</returns>
|
|
/// <exception cref="OperationCanceledException">The <paramref name="cancellationToken"/> was cancelled before the <paramref name="task"/> completed, or the <paramref name="task"/> raised an <see cref="OperationCanceledException"/>.</exception>
|
|
public static TResult WaitAndUnwrapException<TResult>(this Task<TResult> task, CancellationToken cancellationToken)
|
|
{
|
|
if (task == null)
|
|
throw new ArgumentNullException(nameof(task));
|
|
try
|
|
{
|
|
task.Wait(cancellationToken);
|
|
return task.Result;
|
|
}
|
|
catch (AggregateException ex)
|
|
{
|
|
ExceptionDispatchInfo.Capture(ex).Throw();
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Waits for the task to complete, but does not raise task exceptions. The task exception (if any) is unobserved.
|
|
/// </summary>
|
|
/// <param name="task">The task. May not be <c>null</c>.</param>
|
|
public static void WaitWithoutException(this Task task)
|
|
{
|
|
if (task == null)
|
|
throw new ArgumentNullException(nameof(task));
|
|
try
|
|
{
|
|
task.Wait();
|
|
}
|
|
catch (AggregateException)
|
|
{
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Waits for the task to complete, but does not raise task exceptions. The task exception (if any) is unobserved.
|
|
/// </summary>
|
|
/// <param name="task">The task. May not be <c>null</c>.</param>
|
|
/// <param name="cancellationToken">A cancellation token to observe while waiting for the task to complete.</param>
|
|
/// <exception cref="OperationCanceledException">The <paramref name="cancellationToken"/> was cancelled before the <paramref name="task"/> completed.</exception>
|
|
public static void WaitWithoutException(this Task task, CancellationToken cancellationToken)
|
|
{
|
|
if (task == null)
|
|
throw new ArgumentNullException(nameof(task));
|
|
try
|
|
{
|
|
task.Wait(cancellationToken);
|
|
}
|
|
catch (AggregateException)
|
|
{
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
}
|
|
}
|
|
|
|
}
|
|
} |