Skip to content

Commit 197f4c5

Browse files
author
lrhh123
committed
fix: 关闭时残留后台进程
1 parent dc53ff4 commit 197f4c5

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/main/main.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ import Server from './backend/backend';
2222
let mainWindow: BrowserWindow | null = null;
2323
let backendServiceManager: BackendServiceManager | null = null;
2424

25+
const stopBackendServiceManager = async () => {
26+
if (backendServiceManager) {
27+
await backendServiceManager.stop();
28+
}
29+
};
30+
2531
// 修复 GPU process isn't usable. Goodbye. 错误
2632
// https://learn.microsoft.com/en-us/answers/questions/1193062/how-to-fix-electron-program-gpu-process-isnt-usabl
2733
app.commandLine.appendSwitch('no-sandbox');
@@ -31,23 +37,23 @@ app.on('window-all-closed', async () => {
3137
// Respect the OSX convention of having the application in memory even
3238
// after all windows have been closed
3339
console.log('window-all-closed');
34-
await backendServiceManager?.stop();
40+
await stopBackendServiceManager();
3541
if (process.platform !== 'darwin') {
3642
app.quit();
3743
}
3844
});
3945

4046
app.on('before-quit', async () => {
4147
console.log('before-quit');
42-
await backendServiceManager?.stop();
48+
await stopBackendServiceManager();
4349
});
4450

4551
const originalUncaughtException = process.listeners('uncaughtException').pop();
4652
process.removeAllListeners('uncaughtException');
4753
process.on('uncaughtException', async (error, origin) => {
4854
console.error('An error occurred in the main process:', error);
4955
console.error(error.stack);
50-
await backendServiceManager?.stop();
56+
await stopBackendServiceManager();
5157
originalUncaughtException?.(error, origin);
5258
});
5359

@@ -144,17 +150,21 @@ const createWindow = async () => {
144150

145151
mainWindow.on('closed', () => {
146152
mainWindow = null;
153+
// 确保所有窗口关闭后退出应用
154+
if (BrowserWindow.getAllWindows().length === 0) {
155+
app.quit();
156+
}
147157
});
148158

149-
mainWindow.on('close', async (event) => {
150-
event.preventDefault(); // 阻止默认行为
151-
await backendServiceManager?.stop();
159+
mainWindow.on('close', async () => {
160+
// 停止后台服务
161+
await stopBackendServiceManager();
162+
// 关闭所有窗口
152163
BrowserWindow.getAllWindows().forEach((win) => {
153164
if (win !== mainWindow) {
154165
win.close();
155166
}
156167
});
157-
app.quit(); // 关闭应用
158168
});
159169

160170
const menuBuilder = new MenuBuilder(mainWindow);

0 commit comments

Comments
 (0)