Skip to content

Commit 74db965

Browse files
committed
Enable termination of transform process
1 parent ac02124 commit 74db965

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

src/Microsoft.OpenApi.Hidi/Program.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System;
45
using System.CommandLine;
5-
using System.CommandLine.Builder;
6-
using System.CommandLine.Hosting;
76
using System.CommandLine.Parsing;
8-
7+
using System.Diagnostics;
98
using System.IO;
9+
using System.Threading;
1010
using System.Threading.Tasks;
11-
using Microsoft.Extensions.Configuration;
12-
using Microsoft.Extensions.Hosting;
1311
using Microsoft.Extensions.Logging;
1412
using Microsoft.OpenApi.Hidi.Handlers;
1513

@@ -19,6 +17,9 @@ static class Program
1917
{
2018
static async Task Main(string[] args)
2119
{
20+
// subscribe to CancelKeyPress event to listen for termination requests from users through Ctrl+C or Ctrl+Break keys
21+
Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPressEvent);
22+
2223
var rootCommand = new RootCommand() {
2324
};
2425

@@ -127,5 +128,29 @@ static async Task Main(string[] args)
127128
//// Wait for logger to write messages to the console before exiting
128129
await Task.Delay(10);
129130
}
131+
132+
/// <summary>
133+
/// This event is raised when the user presses either of the two breaking key combinations: Ctrl+C or Ctrl+Break keys.
134+
/// </summary>
135+
/// <param name="sender"></param>
136+
/// <param name="eventArgs"></param>
137+
private static void Console_CancelKeyPressEvent(object sender, ConsoleCancelEventArgs eventArgs)
138+
{
139+
if ((eventArgs.SpecialKey == ConsoleSpecialKey.ControlC) || (eventArgs.SpecialKey == ConsoleSpecialKey.ControlBreak))
140+
{
141+
Console.WriteLine("CTRL+C pressed, aborting current process...");
142+
Thread.Sleep(5000);
143+
144+
if (Process.GetCurrentProcess().HasExited)
145+
{
146+
Console.WriteLine("Process has already exited.");
147+
}
148+
else
149+
{
150+
Console.WriteLine("Process has not exited, attempting to kill process...");
151+
Process.GetCurrentProcess().Kill();
152+
}
153+
}
154+
}
130155
}
131156
}

0 commit comments

Comments
 (0)