Skip to content

Commit 6adc977

Browse files
committed
Fix #2739: OS check for platform specific event
1 parent 2b6d7c7 commit 6adc977

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/BenchmarkDotNet/Helpers/DisposeAtProcessTermination.cs

Lines changed: 14 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,15 @@ namespace BenchmarkDotNet.Helpers
2728
/// </remarks>
2829
public abstract class DisposeAtProcessTermination : IDisposable
2930
{
30-
public DisposeAtProcessTermination()
31+
protected DisposeAtProcessTermination()
3132
{
32-
Console.CancelKeyPress += OnCancelKeyPress;
3333
AppDomain.CurrentDomain.ProcessExit += OnProcessExit;
34+
if (!(OsDetector.IsAndroid() || OsDetector.IsIOS() || OsDetector.IsTvOS()))
35+
{
36+
// Cancel key presses are not supported by .NET or do not exist for these
37+
Console.CancelKeyPress += OnCancelKeyPress;
38+
}
39+
3440
// It does not make sense to include a Finalizer. We do not manage any native resource and:
3541
// as we are subscribed to static events, it would never be called.
3642
}
@@ -50,8 +56,12 @@ private void OnCancelKeyPress(object? sender, ConsoleCancelEventArgs e)
5056

5157
public virtual void Dispose()
5258
{
53-
Console.CancelKeyPress -= OnCancelKeyPress;
5459
AppDomain.CurrentDomain.ProcessExit -= OnProcessExit;
60+
if (!(OsDetector.IsAndroid() || OsDetector.IsIOS() || OsDetector.IsTvOS()))
61+
{
62+
// Cancel key presses are not supported by .NET or do not exist for these
63+
Console.CancelKeyPress -= OnCancelKeyPress;
64+
}
5565
}
5666
}
5767
}

0 commit comments

Comments
 (0)