diff --git a/GFramework.Core/extensions/AsyncExtensions.cs b/GFramework.Core/extensions/AsyncExtensions.cs index 437752c..edee903 100644 --- a/GFramework.Core/extensions/AsyncExtensions.cs +++ b/GFramework.Core/extensions/AsyncExtensions.cs @@ -36,13 +36,22 @@ public static class AsyncExtensions if (completedTask == delayTask) { - // 优先检查外部取消令牌,若已取消则抛出 OperationCanceledException cancellationToken.ThrowIfCancellationRequested(); throw new TimeoutException($"操作在 {timeout.TotalSeconds} 秒后超时"); } - await timeoutCts.CancelAsync(); - return await task; + await linkedCts.CancelAsync(); + try + { + await task; + } + catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested && + timeoutCts.Token.IsCancellationRequested) + { + // ignore + } + + return task.Result; } /// @@ -69,13 +78,20 @@ public static class AsyncExtensions if (completedTask == delayTask) { - // 优先检查外部取消令牌,若已取消则抛出 OperationCanceledException cancellationToken.ThrowIfCancellationRequested(); throw new TimeoutException($"操作在 {timeout.TotalSeconds} 秒后超时"); } - await timeoutCts.CancelAsync(); - await task; + await linkedCts.CancelAsync(); + try + { + await task; + } + catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested && + timeoutCts.Token.IsCancellationRequested) + { + // ignore + } } /// @@ -86,6 +102,7 @@ public static class AsyncExtensions /// 最大重试次数 /// 重试间隔 /// 判断是否应该重试的函数,默认对所有异常重试 + /// 当为 true 时直接抛出原始异常,否则包装为 AggregateException /// 任务结果 /// 当 taskFactory 为 null 时抛出 /// 当 maxRetries 小于 0 时抛出 @@ -99,7 +116,8 @@ public static class AsyncExtensions this Func> taskFactory, int maxRetries, TimeSpan delay, - Func? shouldRetry = null) + Func? shouldRetry = null, + bool throwOriginal = false) { ArgumentNullException.ThrowIfNull(taskFactory); @@ -123,6 +141,9 @@ public static class AsyncExtensions } else { + if (throwOriginal) + throw; + throw new AggregateException($"操作在 {attempt} 次重试后仍然失败", ex); } } diff --git a/GFramework.Core/extensions/NumericExtensions.cs b/GFramework.Core/extensions/NumericExtensions.cs index 544d6fe..f2080db 100644 --- a/GFramework.Core/extensions/NumericExtensions.cs +++ b/GFramework.Core/extensions/NumericExtensions.cs @@ -23,10 +23,6 @@ public static class NumericExtensions /// public static T Clamp(this T value, T min, T max) where T : IComparable { - ArgumentNullException.ThrowIfNull(value); - ArgumentNullException.ThrowIfNull(min); - ArgumentNullException.ThrowIfNull(max); - if (min.CompareTo(max) > 0) throw new ArgumentException($"最小值 ({min}) 不能大于最大值 ({max})"); @@ -59,10 +55,6 @@ public static class NumericExtensions /// public static bool Between(this T value, T min, T max, bool inclusive = true) where T : IComparable { - ArgumentNullException.ThrowIfNull(value); - ArgumentNullException.ThrowIfNull(min); - ArgumentNullException.ThrowIfNull(max); - if (min.CompareTo(max) > 0) throw new ArgumentException($"最小值 ({min}) 不能大于最大值 ({max})"); @@ -109,4 +101,4 @@ public static class NumericExtensions return (value - from) / (to - from); } -} +} \ No newline at end of file