@@ -22,6 +22,12 @@ import Server from './backend/backend';
2222let mainWindow : BrowserWindow | null = null ;
2323let 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
2733app . 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
4046app . on ( 'before-quit' , async ( ) => {
4147 console . log ( 'before-quit' ) ;
42- await backendServiceManager ?. stop ( ) ;
48+ await stopBackendServiceManager ( ) ;
4349} ) ;
4450
4551const originalUncaughtException = process . listeners ( 'uncaughtException' ) . pop ( ) ;
4652process . removeAllListeners ( 'uncaughtException' ) ;
4753process . 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