From: Andrew Au Date: Sat, 29 Jun 2019 01:47:37 +0000 (-0700) Subject: Using CancellationToken in dotnet-trace (#367) X-Git-Tag: submit/tizen/20190813.035844~4^2^2~16 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c3323496d4a41dcaa2271bc5a39bb9cad0fefb20;p=platform%2Fcore%2Fdotnet%2Fdiagnostics.git Using CancellationToken in dotnet-trace (#367) --- diff --git a/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs b/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs index be8cfd7fd..dbd329d7e 100644 --- a/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs +++ b/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs @@ -6,6 +6,7 @@ using Microsoft.Diagnostics.Tools.RuntimeClient; using System; using System.Collections.Generic; using System.CommandLine; +using System.CommandLine.Binding; using System.CommandLine.Rendering; using System.Diagnostics; using System.IO; @@ -17,9 +18,12 @@ namespace Microsoft.Diagnostics.Tools.Trace { internal static class CollectCommandHandler { + delegate Task CollectDelegate(CancellationToken ct, IConsole console, int processId, FileInfo output, uint buffersize, string providers, string profile, TraceFileFormat format); + /// /// Collects a diagnostic trace from a currently running process. /// + /// The cancellation token /// /// The process to collect the trace from. /// The output path for the collected trace data. @@ -28,7 +32,7 @@ namespace Microsoft.Diagnostics.Tools.Trace /// A named pre-defined set of provider configurations that allows common tracing scenarios to be specified succinctly. /// The desired format of the created trace file. /// - private static async Task Collect(IConsole console, int processId, FileInfo output, uint buffersize, string providers, string profile, TraceFileFormat format) + private static async Task Collect(CancellationToken ct, IConsole console, int processId, FileInfo output, uint buffersize, string providers, string profile, TraceFileFormat format) { try { @@ -96,6 +100,8 @@ namespace Microsoft.Diagnostics.Tools.Trace var failed = false; var terminated = false; + ct.Register(() => shouldExit.Set()); + ulong sessionId = 0; using (Stream stream = EventPipeClient.CollectTracing(processId, configuration, out sessionId)) using (VirtualTerminalMode vTermMode = VirtualTerminalMode.TryEnable()) @@ -149,10 +155,6 @@ namespace Microsoft.Diagnostics.Tools.Trace collectingTask.Start(); Console.Out.WriteLine("Press or to exit..."); - Console.CancelKeyPress += (sender, args) => { - args.Cancel = true; - shouldExit.Set(); - }; do { while (!Console.KeyAvailable && !shouldExit.WaitOne(250)) { } @@ -238,7 +240,7 @@ namespace Microsoft.Diagnostics.Tools.Trace ProfileOption(), CommonOptions.FormatOption(), }, - handler: System.CommandLine.Invocation.CommandHandler.Create(Collect)); + handler: HandlerDescriptor.FromDelegate((CollectDelegate)Collect).GetCommandHandler()); private static uint DefaultCircularBufferSizeInMB => 256;