Add Ctrl-C handler for dotnet-counters (#364)
authorSung Yoon Whang <suwhang@microsoft.com>
Thu, 27 Jun 2019 04:36:46 +0000 (21:36 -0700)
committerGitHub <noreply@github.com>
Thu, 27 Jun 2019 04:36:46 +0000 (21:36 -0700)
src/Tools/dotnet-counters/CounterMonitor.cs

index 379abc9e16637bc4f60de7d73eed0b7b7a858bb2..399fe15d72335d1ed2fecff78d1637909052b955 100644 (file)
@@ -65,6 +65,19 @@ namespace Microsoft.Diagnostics.Tools.Counters
             }
         }
 
+        private void StopMonitor()
+        {
+            try
+            {
+                EventPipeClient.StopTracing(_processId, _sessionId);
+            }
+            catch (EndOfStreamException ex)
+            {
+                // If the app we're monitoring exits abruptly, this may throw in which case we just swallow the exception and exit gracefully.
+                Debug.WriteLine($"[ERROR] {ex.ToString()}");
+            }
+        }
+
         public async Task<int> Monitor(CancellationToken ct, List<string> counter_list, IConsole console, int processId, int refreshInterval)
         {
             try
@@ -152,7 +165,7 @@ namespace Microsoft.Diagnostics.Tools.Counters
                 providerString = sb.ToString();
             }
 
-            var shouldExit = new ManualResetEvent(false);
+            ManualResetEvent shouldExit = new ManualResetEvent(false);
             var terminated = false;
             writer.InitializeDisplay();
 
@@ -181,12 +194,17 @@ namespace Microsoft.Diagnostics.Tools.Counters
             });
 
             monitorTask.Start();
-            while(true)
+            Console.CancelKeyPress += (sender, args) => {
+                args.Cancel = true;
+                shouldExit.Set();
+            };
+            while(!shouldExit.WaitOne(250))
             {
                 while (true)
                 {
                     if (shouldExit.WaitOne(250))
                     {
+                        StopMonitor();
                         return 0;
                     }
                     if (Console.KeyAvailable)
@@ -208,18 +226,9 @@ namespace Microsoft.Diagnostics.Tools.Counters
                     pauseCmdSet = false;
                 }
             }
-
             if (!terminated)
             {
-                try
-                {
-                    EventPipeClient.StopTracing(_processId, _sessionId);    
-                }
-                catch (EndOfStreamException ex)
-                {
-                    // If the app we're monitoring exits abruptly, this may throw in which case we just swallow the exception and exit gracefully.
-                    Debug.WriteLine($"[ERROR] {ex.ToString()}");
-                } 
+                StopMonitor();
             }
             
             return await Task.FromResult(0);