Skip to content

Commit

Permalink
fix(Studio): Gracefull process termination on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Nov 29, 2024
1 parent 5f4fb25 commit 94b341a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Studio/CelesteStudio/Studio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public Studio(string[] args, Action<Window> windowCreationCallback) {

// Close other Studio instances to avoid conflicts
foreach (var process in Process.GetProcesses().Where(process => process.ProcessName is "CelesteStudio" or "CelesteStudio.WPF" or "CelesteStudio.GTK" or "CelesteStudio.Mac" or "Celeste Studio")) {
if (process.Id == Process.GetCurrentProcess().Id) {
if (process.Id == Environment.ProcessId) {
continue;
}

Expand Down
16 changes: 7 additions & 9 deletions StudioCommunication/Util/ProcessHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ public static void Terminate(this Process process) {
}

// Windows
FreeConsole();
AttachConsole((uint)process.Id);
GenerateConsoleCtrlEvent(0, 0);
if (process.MainWindowHandle != IntPtr.Zero) {
PostMessage(process.MainWindowHandle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
}

[DllImport("kernel32.dll")]
private static extern bool GenerateConsoleCtrlEvent(uint dwCtrlEvent, uint dwProcessGroupId);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AttachConsole(uint dwProcessId);
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
private static extern bool FreeConsole();
[DllImport("user32.dll")]
private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

private const uint WM_CLOSE = 0x0010;
}

0 comments on commit 94b341a

Please sign in to comment.