Skip to content

Commit 69eb802

Browse files
author
John Salem
authored
Add ctrl c handler and modify enter key handler to be non-blocking (#155)
* Add ctrl c handler and modify enter key handler to be non-blocking
1 parent c66e71a commit 69eb802

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.CommandLine.Invocation;
99
using System.Diagnostics;
1010
using System.IO;
11+
using System.Threading;
1112
using System.Threading.Tasks;
1213

1314
namespace Microsoft.Diagnostics.Tools.Trace
@@ -35,6 +36,8 @@ public static async Task<int> Collect(IConsole console, int processId, string ou
3536
outputPath: null, // Not used on the streaming scenario.
3637
Extensions.ToProviders(providers));
3738

39+
var shouldExit = new ManualResetEvent(false);
40+
3841
ulong sessionId = 0;
3942
using (Stream stream = EventPipeClient.CollectTracing(processId, configuration, out sessionId))
4043
{
@@ -67,8 +70,15 @@ public static async Task<int> Collect(IConsole console, int processId, string ou
6770
});
6871
collectingTask.Start();
6972

70-
Console.Out.WriteLine("press <Enter> to exit...");
71-
while (Console.ReadKey().Key != ConsoleKey.Enter) { }
73+
Console.Out.WriteLine("press <Enter> or <Ctrl+c> to exit...");
74+
System.Console.CancelKeyPress += (sender, args) => {
75+
args.Cancel = true;
76+
shouldExit.Set();
77+
};
78+
79+
do {
80+
while (!Console.KeyAvailable && !shouldExit.WaitOne(250)) { }
81+
} while (!shouldExit.WaitOne(0) && Console.ReadKey(true).Key != ConsoleKey.Enter);
7282

7383
EventPipeClient.StopTracing(processId, sessionId);
7484
collectingTask.Wait();

0 commit comments

Comments
 (0)