Skip to content

Commit

Permalink
fix: 关闭时残留后台进程
Browse files Browse the repository at this point in the history
  • Loading branch information
lrhh123 committed Jul 2, 2024
1 parent dc53ff4 commit 197f4c5
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ import Server from './backend/backend';
let mainWindow: BrowserWindow | null = null;
let backendServiceManager: BackendServiceManager | null = null;

const stopBackendServiceManager = async () => {
if (backendServiceManager) {
await backendServiceManager.stop();
}
};

// 修复 GPU process isn't usable. Goodbye. 错误
// https://learn.microsoft.com/en-us/answers/questions/1193062/how-to-fix-electron-program-gpu-process-isnt-usabl
app.commandLine.appendSwitch('no-sandbox');
Expand All @@ -31,23 +37,23 @@ app.on('window-all-closed', async () => {
// Respect the OSX convention of having the application in memory even
// after all windows have been closed
console.log('window-all-closed');
await backendServiceManager?.stop();
await stopBackendServiceManager();
if (process.platform !== 'darwin') {
app.quit();
}
});

app.on('before-quit', async () => {
console.log('before-quit');
await backendServiceManager?.stop();
await stopBackendServiceManager();
});

const originalUncaughtException = process.listeners('uncaughtException').pop();
process.removeAllListeners('uncaughtException');
process.on('uncaughtException', async (error, origin) => {
console.error('An error occurred in the main process:', error);
console.error(error.stack);
await backendServiceManager?.stop();
await stopBackendServiceManager();
originalUncaughtException?.(error, origin);
});

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

mainWindow.on('closed', () => {
mainWindow = null;
// 确保所有窗口关闭后退出应用
if (BrowserWindow.getAllWindows().length === 0) {
app.quit();
}
});

mainWindow.on('close', async (event) => {
event.preventDefault(); // 阻止默认行为
await backendServiceManager?.stop();
mainWindow.on('close', async () => {
// 停止后台服务
await stopBackendServiceManager();
// 关闭所有窗口
BrowserWindow.getAllWindows().forEach((win) => {
if (win !== mainWindow) {
win.close();
}
});
app.quit(); // 关闭应用
});

const menuBuilder = new MenuBuilder(mainWindow);
Expand Down

0 comments on commit 197f4c5

Please sign in to comment.