【问题标题】:Shutdown Electron Client gracefully from external从外部优雅地关闭 Electron 客户端
【发布时间】:2021-04-15 13:26:46
【问题描述】:

我们有一个 Electron 客户端应用程序,必须通过公司范围的软件管理系统进行部署和更新。

目前正在运行的 Electron 客户端应用程序在安装更新之前被简单地“杀死”。

在 Windows 10 下,有没有办法告诉 Electron 客户端正常关闭(内部应用程序关闭事件或其他事件应该运行)

【问题讨论】:

    标签: electron


    【解决方案1】:

    Electron 可以在 Windows 的后台进程中处理 graceful-exit,在 Linux 中可以处理 SIGTERM

    要求“礼貌”关闭应用程序:

    • 使用taskkill 而不使用/f - details。 (窗户)
    • 使用pkill -TERM <proc-name> 而不是kill details (linux)
    // Quit when all windows are closed.
    app.on('window-all-closed', () => {
      // On macOS it is common for applications and their menu bar
      // to stay active until the user quits explicitly with Cmd + Q
      if (process.platform !== 'darwin') {
        console.log('if you see this message - all windows was closed')
        app.quit()
      }
    })
    
    app.on('activate', () => {
      // On macOS it's common to re-create a window in the app when the
      // dock icon is clicked and there are no other windows open.
      if (BrowserWindow.getAllWindows().length === 0) createWindow()
    })
    
    // This method will be called when Electron has finished
    // initialization and is ready to create browser windows.
    // Some APIs can only be used after this event occurs.
    app.on('ready', async () => {
      createWindow()
    })
    
    // Exit cleanly on request from parent process.
    if (process.platform === 'win32') {
      // this message usually fires in dev-mode from the parent process
      process.on('message', (data) => {
        if (data === 'graceful-exit') {
          console.log('if you see this message - graceful-exit fired')
          app.quit()
        }
      })
    } else {
      process.on('SIGTERM', () => {
        console.log('if you see this message - SIGTERM fired')
        app.quit()
      })
    }
    

    【讨论】:

    • 在 Linux 下运行良好。 windows下没有消息,进程没有被杀死而是在等待。客户没有反应。我也尝试实现 SIGINT stackoverflow.com/questions/10021373/…
    • @Nabor 我已经更新了示例。您还应该处理“window-all-closed”。
    • window-all-closed 如果所有窗口都关闭并且应用程序最小化到托盘,也会触发。在这种情况下退出应用程序会破坏其他用例。所以如果实在没有办法,我会在app用户主目录中实现一个文件监听器。如果在那里创建了特定文件,应用程序将正常关闭自身。
    猜你喜欢
    • 1970-01-01
    • 2014-04-29
    • 2021-04-21
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 2013-06-27
    • 2015-09-29
    相关资源
    最近更新 更多