forked from dotnet/BenchmarkDotNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stop the ETW session on Ctrl+C + restore console colors ;), fixes dot…
…net#729 (dotnet#761) * stop the ETW session on Ctrl+C + restore console colors ;), fixes dotnet#729 * post code review fixes, dotnet#729
- Loading branch information
1 parent
e4461c6
commit 5f65863
Showing
5 changed files
with
113 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/BenchmarkDotNet.Diagnostics.Windows/NativeWindowsConsoleHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace BenchmarkDotNet.Diagnostics.Windows | ||
{ | ||
// we need this class because when the Console window gets closed (by simply pressing x) | ||
// the managed event is not raised for CTRL_CLOSE_EVENT, more https://stackoverflow.com/questions/474679/capture-console-exit-c-sharp | ||
internal static class NativeWindowsConsoleHelper | ||
{ | ||
private enum CtrlType | ||
{ | ||
CTRL_C_EVENT = 0, | ||
CTRL_BREAK_EVENT = 1, | ||
CTRL_CLOSE_EVENT = 2, | ||
CTRL_LOGOFF_EVENT = 5, | ||
CTRL_SHUTDOWN_EVENT = 6 | ||
} | ||
|
||
private delegate bool ConsoleCtrlHandler(CtrlType sig); | ||
|
||
[DllImport("Kernel32")] | ||
private static extern bool SetConsoleCtrlHandler(ConsoleCtrlHandler handler, bool add); | ||
|
||
private static event EventHandler<ConsoleCancelEventArgs> onExit; | ||
|
||
public static event EventHandler<ConsoleCancelEventArgs> OnExit | ||
{ | ||
add | ||
{ | ||
if (Portability.RuntimeInformation.IsWindows()) | ||
{ | ||
SetConsoleCtrlHandler(NativeHandler, true); | ||
|
||
onExit += value; | ||
} | ||
} | ||
remove | ||
{ | ||
if (Portability.RuntimeInformation.IsWindows()) | ||
{ | ||
SetConsoleCtrlHandler(NativeHandler, false); | ||
|
||
onExit -= value; | ||
} | ||
} | ||
} | ||
|
||
private static bool NativeHandler(CtrlType sig) | ||
{ | ||
switch (sig) | ||
{ | ||
case CtrlType.CTRL_C_EVENT: | ||
case CtrlType.CTRL_BREAK_EVENT: | ||
case CtrlType.CTRL_LOGOFF_EVENT: | ||
case CtrlType.CTRL_SHUTDOWN_EVENT: | ||
case CtrlType.CTRL_CLOSE_EVENT: | ||
onExit?.Invoke(null, null); | ||
return false; // if we return true from here, the event is marked as handled and the managed handler is not fired | ||
default: | ||
return false; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters