From 49a8023f55bda9432141713b391e0a9111d9c0ac Mon Sep 17 00:00:00 2001 From: Sergey Odinokov Date: Tue, 26 Nov 2024 10:28:15 +0700 Subject: [PATCH] Revert "Prefer using CancellationToken.WaitHandle again" This reverts commit 4ced6444e176b0435759f75cecbbf7a762a70661. --- src/Hangfire.Core/Common/CancellationTokenExtentions.cs | 6 +++--- src/Hangfire.Core/Processing/TaskExtensions.cs | 4 +++- src/Hangfire.Core/Server/CoreBackgroundJobPerformer.cs | 3 ++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Hangfire.Core/Common/CancellationTokenExtentions.cs b/src/Hangfire.Core/Common/CancellationTokenExtentions.cs index 904c67878..97da8e097 100644 --- a/src/Hangfire.Core/Common/CancellationTokenExtentions.cs +++ b/src/Hangfire.Core/Common/CancellationTokenExtentions.cs @@ -28,7 +28,6 @@ public static class CancellationTokenExtentions /// on cancellation token registration and avoids using the /// property as it may lead to high CPU issues. /// - [Obsolete("CancellationToken.WaitHandle is now preferred, since early days of .NET Core passed. Will be removed in 2.0.0.")] public static CancellationEvent GetCancellationEvent(this CancellationToken cancellationToken) { return new CancellationEvent(cancellationToken); @@ -57,8 +56,10 @@ public static void WaitOrThrow(this CancellationToken cancellationToken, TimeSpa /// public static bool Wait(this CancellationToken cancellationToken, TimeSpan timeout) { + using var cancellationEvent = GetCancellationEvent(cancellationToken); + var stopwatch = Stopwatch.StartNew(); - var waitResult = cancellationToken.WaitHandle.WaitOne(timeout); + var waitResult = cancellationEvent.WaitHandle.WaitOne(timeout); stopwatch.Stop(); var timeoutThreshold = TimeSpan.FromMilliseconds(1000); @@ -83,7 +84,6 @@ public static bool Wait(this CancellationToken cancellationToken, TimeSpan timeo return waitResult; } - [Obsolete("CancellationToken.WaitHandle is now preferred, since early days of .NET Core passed. Will be removed in 2.0.0.")] public sealed class CancellationEvent : IDisposable { private static readonly Action SetEventCallback = SetEvent; diff --git a/src/Hangfire.Core/Processing/TaskExtensions.cs b/src/Hangfire.Core/Processing/TaskExtensions.cs index 9eec8651f..4f21b3e56 100644 --- a/src/Hangfire.Core/Processing/TaskExtensions.cs +++ b/src/Hangfire.Core/Processing/TaskExtensions.cs @@ -36,7 +36,9 @@ public static bool WaitOne([NotNull] this WaitHandle waitHandle, TimeSpan timeou token.ThrowIfCancellationRequested(); - var waitHandles = new[] { waitHandle, token.WaitHandle }; + using var ev = token.GetCancellationEvent(); + + var waitHandles = new[] { waitHandle, ev.WaitHandle }; var stopwatch = Stopwatch.StartNew(); var waitResult = WaitHandle.WaitAny(waitHandles, timeout); diff --git a/src/Hangfire.Core/Server/CoreBackgroundJobPerformer.cs b/src/Hangfire.Core/Server/CoreBackgroundJobPerformer.cs index 74122a189..955c7906d 100644 --- a/src/Hangfire.Core/Server/CoreBackgroundJobPerformer.cs +++ b/src/Hangfire.Core/Server/CoreBackgroundJobPerformer.cs @@ -182,6 +182,7 @@ private static object InvokeOnTaskPump(PerformContext context, Tuple