为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

LEATH 2022-08-27 14:40:31
我有一点问题,我希望有人可以帮助我。我有一个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();});有人可以帮我吗?
查看完整描述

3 回答

?
函数式编程

TA贡献1807条经验 获得超9个赞

这是因为您正在使用 ,这是预期的行为。concurrently

当您关闭窗口(并在macOS上退出程序)时,电子过程确实会停止,但是您在终端中发出的命令仍在运行,因为您仍在运行 。react-scripts

查看脚本,您说要运行命令和 .当您关闭电子应用程序时,它会告诉您该过程已结束()。但是,您只结束了您创建的 2 个进程中的 1 个。创建的进程仍在运行,因此终端控制权不会返回给您。electron-devnpm startwait-on http://localhost:3000 && electron .\wait-on http://localhost:3000 && electron . exited with code 0npm start

npm start执行命令 ,该命令设置开发环境并启动服务器。您有几个选项可以杀死该过程,CTRL + C是其中最简单的。react-scripts start

打包应用程序时,您不会遇到此问题,当用户关闭窗口(或在macOS上退出程序)时,应用程序将完全关闭


查看完整回答
反对 回复 2022-08-27
?
拉风的咖菲猫

TA贡献1995条经验 获得超2个赞

也许对于谁提出了这个问题来说为时已晚,但对于仍在寻找解决方案的其他人来说:

可以使用 npm-run-all。

以下是此软件包的文档。


查看完整回答
反对 回复 2022-08-27
?
慕田峪4524236

TA贡献1875条经验 获得超5个赞

解决此问题的最优雅方法是在脚本中使用 / 选项。--kill-others-kconcurrently


在我的包文件中,在脚本下:


"dev": "concurrently   \"npm run client\" \"wait-on tcp:3000 && electron .\" -k",


在相关进程的任何类型的退出中,其他进程也将停止。这可以由 进一步控制,如其文档中所述:--kill-others-on-fail


https://www.npmjs.com/package/concurrently


Killing 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]


查看完整回答
反对 回复 2022-08-27
  • 3 回答
  • 0 关注
  • 424 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信