Skip to content
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;
}
}
}
}