Skip to content

Commit 49a8023

Browse files
committed
Revert "Prefer using CancellationToken.WaitHandle again"
This reverts commit 4ced644.
1 parent 9685dd9 commit 49a8023

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

Diff for: src/Hangfire.Core/Common/CancellationTokenExtentions.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public static class CancellationTokenExtentions
2828
/// on cancellation token registration and avoids using the <see cref="CancellationToken.WaitHandle"/>
2929
/// property as it may lead to high CPU issues.
3030
/// </summary>
31-
[Obsolete("CancellationToken.WaitHandle is now preferred, since early days of .NET Core passed. Will be removed in 2.0.0.")]
3231
public static CancellationEvent GetCancellationEvent(this CancellationToken cancellationToken)
3332
{
3433
return new CancellationEvent(cancellationToken);
@@ -57,8 +56,10 @@ public static void WaitOrThrow(this CancellationToken cancellationToken, TimeSpa
5756
/// </summary>
5857
public static bool Wait(this CancellationToken cancellationToken, TimeSpan timeout)
5958
{
59+
using var cancellationEvent = GetCancellationEvent(cancellationToken);
60+
6061
var stopwatch = Stopwatch.StartNew();
61-
var waitResult = cancellationToken.WaitHandle.WaitOne(timeout);
62+
var waitResult = cancellationEvent.WaitHandle.WaitOne(timeout);
6263
stopwatch.Stop();
6364

6465
var timeoutThreshold = TimeSpan.FromMilliseconds(1000);
@@ -83,7 +84,6 @@ public static bool Wait(this CancellationToken cancellationToken, TimeSpan timeo
8384
return waitResult;
8485
}
8586

86-
[Obsolete("CancellationToken.WaitHandle is now preferred, since early days of .NET Core passed. Will be removed in 2.0.0.")]
8787
public sealed class CancellationEvent : IDisposable
8888
{
8989
private static readonly Action<object> SetEventCallback = SetEvent;

Diff for: src/Hangfire.Core/Processing/TaskExtensions.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public static bool WaitOne([NotNull] this WaitHandle waitHandle, TimeSpan timeou
3636

3737
token.ThrowIfCancellationRequested();
3838

39-
var waitHandles = new[] { waitHandle, token.WaitHandle };
39+
using var ev = token.GetCancellationEvent();
40+
41+
var waitHandles = new[] { waitHandle, ev.WaitHandle };
4042

4143
var stopwatch = Stopwatch.StartNew();
4244
var waitResult = WaitHandle.WaitAny(waitHandles, timeout);

Diff for: src/Hangfire.Core/Server/CoreBackgroundJobPerformer.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ private static object InvokeOnTaskPump(PerformContext context, Tuple<MethodInfo,
182182
try
183183
{
184184
using (var syncContext = new InlineSynchronizationContext())
185+
using (var cancellationEvent = context.CancellationToken.ShutdownToken.GetCancellationEvent())
185186
{
186187
SynchronizationContext.SetSynchronizationContext(syncContext);
187188

@@ -191,7 +192,7 @@ private static object InvokeOnTaskPump(PerformContext context, Tuple<MethodInfo,
191192
var task = getTaskFunc(result);
192193
var asyncResult = (IAsyncResult)task;
193194

194-
var waitHandles = new[] { syncContext.WaitHandle, asyncResult.AsyncWaitHandle, context.CancellationToken.ShutdownToken.WaitHandle };
195+
var waitHandles = new[] { syncContext.WaitHandle, asyncResult.AsyncWaitHandle, cancellationEvent.WaitHandle };
195196

196197
while (!asyncResult.IsCompleted && WaitHandle.WaitAny(waitHandles) == 0)
197198
{

0 commit comments

Comments
 (0)