关闭 Electron 应用程序不会停止脚本

我有一点问题,我希望有人可以帮助我。


我有一个Electron + React桌面应用程序,我需要正确处理它的关闭。当我关闭应用程序(单击窗口上的X)时,程序停止,但是,我用于运行程序的终端窗口不会停止。


我用这个脚本来运行程序:


npm run electron-dev

这确实:


"scripts": {

   "start": "react-scripts start",

   "electron-dev": "concurrently \"npm run start\" \"wait-on http://localhost:3000 && electron .\""

 }

我正常运行我的应用程序,当我关闭窗口时,我的终端会:


wait-on http://localhost:3000 && electron . exited with code 0

但是我不能在我的终端上打字,除非我用Control + C杀死程序。


以下是我处理应用程序关闭的方式:


app.on('window-all-closed', () => {

  if (process.platform !== 'darwin') {

     app.quit();

   }

});


app.on('before-quit', () => {

    mainWindow.removeAllListeners('close');

    mainWindow.close();

});

有人可以帮我吗?


LEATH
浏览 548回答 3
3回答

函数式编程

这是因为您正在使用 ,这是预期的行为。concurrently当您关闭窗口(并在macOS上退出程序)时,电子过程确实会停止,但是您在终端中发出的命令仍在运行,因为您仍在运行 。react-scripts查看脚本,您说要运行命令和 .当您关闭电子应用程序时,它会告诉您该过程已结束()。但是,您只结束了您创建的 2 个进程中的 1 个。创建的进程仍在运行,因此终端控制权不会返回给您。electron-devnpm startwait-on http://localhost:3000 && electron .\wait-on http://localhost:3000 && electron . exited with code 0npm startnpm start执行命令 ,该命令设置开发环境并启动服务器。您有几个选项可以杀死该过程,CTRL + C是其中最简单的。react-scripts start打包应用程序时,您不会遇到此问题,当用户关闭窗口(或在macOS上退出程序)时,应用程序将完全关闭

拉风的咖菲猫

也许对于谁提出了这个问题来说为时已晚,但对于仍在寻找解决方案的其他人来说:可以使用 npm-run-all。以下是此软件包的文档。

慕田峪4524236

解决此问题的最优雅方法是在脚本中使用 / 选项。--kill-others-kconcurrently在我的包文件中,在脚本下:"dev": "concurrently   \"npm run client\" \"wait-on tcp:3000 && electron .\" -k",在相关进程的任何类型的退出中,其他进程也将停止。这可以由 进一步控制,如其文档中所述:--kill-others-on-failhttps://www.npmjs.com/package/concurrentlyKilling other processes  -k, --kill-others          kill other processes if one exits or dies [boolean]      --kill-others-on-fail  kill other processes if one exits with non zero                             status code                               [boolean]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript