从外部优雅地关闭Electron客户端

从外部优雅地关闭Electron客户端,electron,Electron,我们有一个电子客户端应用程序,必须通过公司范围的软件管理系统进行部署和更新 目前,运行的Electron客户端应用程序在安装更新之前被简单地“杀死” 在Windows 10下,是否有一种方法可以告诉Electron客户端正常关机(在内部,应用程序关闭事件或其他事件应该运行)Electron可以在Windows的后台进程中处理优雅退出,在Linux的后台进程中处理SIGTERM 要请求“礼貌”应用程序关闭: 使用taskkill而不使用/f-。(窗口) 使用pkill-TERM而不是kill(l

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

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


在Windows 10下,是否有一种方法可以告诉Electron客户端正常关机(在内部,应用程序关闭事件或其他事件应该运行)

Electron
可以在Windows的后台进程中处理
优雅退出
,在Linux的后台进程中处理
SIGTERM

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

  • 使用
    taskkill
    而不使用
    /f
    -。(窗口)
  • 使用
    pkill-TERM
    而不是
    kill
    (linux)

在Linux下运行良好。windows下没有消息,进程未终止,但正在等待。客户没有反应。我还尝试实现SIGINT@Nabor,因为我已经更新了示例。您还应该处理“窗口全部关闭”。
如果所有窗口都关闭并且应用程序最小化到最小,则也会触发“窗口全部关闭”
。在此场景中退出应用程序会破坏其他用例。因此,如果真的没有其他方法,我将在应用程序用户主目录中实现一个文件侦听器。如果在那里创建了特定的文件,应用程序将自动关闭。
// 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()
  })
}