1
- using System ;
1
+ using BenchmarkDotNet . Detectors ;
2
+ using System ;
2
3
using System . Runtime . InteropServices ;
3
4
4
5
namespace BenchmarkDotNet . Helpers
@@ -27,10 +28,18 @@ namespace BenchmarkDotNet.Helpers
27
28
/// </remarks>
28
29
public abstract class DisposeAtProcessTermination : IDisposable
29
30
{
30
- public DisposeAtProcessTermination ( )
31
+ private static readonly bool ConsoleSupportsCancelKeyPress
32
+ = ! ( OsDetector . IsAndroid ( ) || OsDetector . IsIOS ( ) || OsDetector . IsTvOS ( ) || Portability . RuntimeInformation . IsWasm ) ;
33
+
34
+ protected DisposeAtProcessTermination ( )
31
35
{
32
- Console . CancelKeyPress += OnCancelKeyPress ;
33
36
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
+
34
43
// It does not make sense to include a Finalizer. We do not manage any native resource and:
35
44
// as we are subscribed to static events, it would never be called.
36
45
}
@@ -50,8 +59,12 @@ private void OnCancelKeyPress(object? sender, ConsoleCancelEventArgs e)
50
59
51
60
public virtual void Dispose ( )
52
61
{
53
- Console . CancelKeyPress -= OnCancelKeyPress ;
54
62
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
+ }
55
68
}
56
69
}
57
70
}
0 commit comments