Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 25b01d2

Browse files
fix: TerminateSafely did not work on Windows (#1012)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 58e2393 commit 25b01d2

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

‎src/Native/Windows.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ private enum CTRL_EVENT : int
5959
private static extern int SHOpenFolderAndSelectItems(IntPtr pidlFolder, int cild, IntPtr apidl, int dwFlags);
6060

6161
[DllImport("kernel32.dll")]
62-
public static extern bool SetConsoleCtrlHandler(IntPtr handlerRoutine, bool add);
62+
private static extern bool AttachConsole(int dwProcessId);
63+
64+
[DllImport("kernel32.dll")]
65+
private static extern bool FreeConsole();
66+
67+
[DllImport("kernel32.dll")]
68+
private static extern bool SetConsoleCtrlHandler(IntPtr handlerRoutine, bool add);
6369

6470
[DllImport("kernel32.dll")]
6571
private static extern bool GenerateConsoleCtrlEvent(int dwCtrlEvent, int dwProcessGroupId);
@@ -225,17 +231,21 @@ public void OpenWithDefaultEditor(string file)
225231

226232
public void TerminateSafely(Process process)
227233
{
228-
if (SetConsoleCtrlHandler(IntPtr.Zero, true))
234+
FreeConsole();
235+
236+
if (AttachConsole(process.Id))
229237
{
238+
SetConsoleCtrlHandler(IntPtr.Zero, true);
230239
try
231240
{
232-
if (GenerateConsoleCtrlEvent((int)CTRL_EVENT.CTRL_C, process.Id))
233-
process.WaitForExit();
241+
if (GenerateConsoleCtrlEvent((int)CTRL_EVENT.CTRL_C, 0))
242+
process.WaitForExit(2000);
234243
}
235-
finally
244+
catch
236245
{
237-
SetConsoleCtrlHandler(IntPtr.Zero,false);
246+
// DO NOTHING
238247
}
248+
SetConsoleCtrlHandler(IntPtr.Zero, false);
239249
}
240250
}
241251

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /