【发布时间】:2021-04-15 13:26:46
【问题描述】:
我们有一个 Electron 客户端应用程序,必须通过公司范围的软件管理系统进行部署和更新。
目前正在运行的 Electron 客户端应用程序在安装更新之前被简单地“杀死”。
在 Windows 10 下,有没有办法告诉 Electron 客户端正常关闭(内部应用程序关闭事件或其他事件应该运行)
【问题讨论】:
标签: electron
我们有一个 Electron 客户端应用程序,必须通过公司范围的软件管理系统进行部署和更新。
目前正在运行的 Electron 客户端应用程序在安装更新之前被简单地“杀死”。
在 Windows 10 下,有没有办法告诉 Electron 客户端正常关闭(内部应用程序关闭事件或其他事件应该运行)
【问题讨论】:
标签: electron
Electron 可以在 Windows 的后台进程中处理 graceful-exit,在 Linux 中可以处理 SIGTERM。
要求“礼貌”关闭应用程序:
// 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()
})
}
【讨论】:
window-all-closed 如果所有窗口都关闭并且应用程序最小化到托盘,也会触发。在这种情况下退出应用程序会破坏其他用例。所以如果实在没有办法,我会在app用户主目录中实现一个文件监听器。如果在那里创建了特定文件,应用程序将正常关闭自身。