Skip to content

Fix #2739: OS check for platform specific event #2741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/BenchmarkDotNet/Helpers/DisposeAtProcessTermination.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using BenchmarkDotNet.Detectors;
using System;
using System.Runtime.InteropServices;

namespace BenchmarkDotNet.Helpers
Expand Down Expand Up @@ -27,10 +28,18 @@ namespace BenchmarkDotNet.Helpers
/// </remarks>
public abstract class DisposeAtProcessTermination : IDisposable
{
public DisposeAtProcessTermination()
private static readonly bool ConsoleSupportsCancelKeyPress
= !(OsDetector.IsAndroid() || OsDetector.IsIOS() || OsDetector.IsTvOS() || Portability.RuntimeInformation.IsWasm);

protected DisposeAtProcessTermination()
{
Console.CancelKeyPress += OnCancelKeyPress;
AppDomain.CurrentDomain.ProcessExit += OnProcessExit;
if (ConsoleSupportsCancelKeyPress)
{
// Cancel key presses are not supported by .NET or do not exist for these
Console.CancelKeyPress += OnCancelKeyPress;
}

// It does not make sense to include a Finalizer. We do not manage any native resource and:
// as we are subscribed to static events, it would never be called.
}
Expand All @@ -50,8 +59,12 @@ private void OnCancelKeyPress(object? sender, ConsoleCancelEventArgs e)

public virtual void Dispose()
{
Console.CancelKeyPress -= OnCancelKeyPress;
AppDomain.CurrentDomain.ProcessExit -= OnProcessExit;
if (ConsoleSupportsCancelKeyPress)
{
// Cancel key presses are not supported by .NET or do not exist for these
Console.CancelKeyPress -= OnCancelKeyPress;
}
}
}
}