1- using System ;
1+ using BenchmarkDotNet . Detectors ;
2+ using System ;
23using System . Runtime . InteropServices ;
34
45namespace 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