Skip to content

Commit 8bf4623

Browse files
authored
Fix #2739: OS check for platform exclusive event (#2741)
1 parent c0bcfb9 commit 8bf4623

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/BenchmarkDotNet/Helpers/DisposeAtProcessTermination.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using BenchmarkDotNet.Detectors;
2+
using System;
23
using System.Runtime.InteropServices;
34

45
namespace BenchmarkDotNet.Helpers
@@ -27,10 +28,18 @@ namespace BenchmarkDotNet.Helpers
2728
/// </remarks>
2829
public abstract class DisposeAtProcessTermination : IDisposable
2930
{
30-
public DisposeAtProcessTermination()
31+
private static readonly bool ConsoleSupportsCancelKeyPress
32+
= !(OsDetector.IsAndroid() || OsDetector.IsIOS() || OsDetector.IsTvOS() || Portability.RuntimeInformation.IsWasm);
33+
34+
protected DisposeAtProcessTermination()
3135
{
32-
Console.CancelKeyPress += OnCancelKeyPress;
3336
AppDomain.CurrentDomain.ProcessExit += OnProcessExit;
37+
if (ConsoleSupportsCancelKeyPress)
38+
{
39+
// Cancel key presses are not supported by .NET or do not exist for these
40+
Console.CancelKeyPress += OnCancelKeyPress;
41+
}
42+
3443
// It does not make sense to include a Finalizer. We do not manage any native resource and:
3544
// as we are subscribed to static events, it would never be called.
3645
}
@@ -50,8 +59,12 @@ private void OnCancelKeyPress(object? sender, ConsoleCancelEventArgs e)
5059

5160
public virtual void Dispose()
5261
{
53-
Console.CancelKeyPress -= OnCancelKeyPress;
5462
AppDomain.CurrentDomain.ProcessExit -= OnProcessExit;
63+
if (ConsoleSupportsCancelKeyPress)
64+
{
65+
// Cancel key presses are not supported by .NET or do not exist for these
66+
Console.CancelKeyPress -= OnCancelKeyPress;
67+
}
5568
}
5669
}
5770
}

0 commit comments

Comments
 (0)