[英]Is there any way in electron to detect a shutdown on Windows?
I tried "session-end", "window-all-closed" to capture the windows shutdown event.我尝试了“会话结束”、“窗口全部关闭”来捕获 windows 关闭事件。 Electron needs to call function before computer gets shutdown.
Electron 需要在计算机关机前调用 function。
win.on("session-end",(event) => {
event.sender.send("appshutdown");
win = null;
console.log('app shutdown - main.js');
});
app.on("window-all-closed", (event) => {
if (process.platform !== "darwin") {
event.sender.send("appshutdown");
app.quit();
}
});
Use node's process exit event to run code when the process is exiting, which happens when a system shutdown or restart occurs.使用节点的进程退出事件在进程退出时运行代码,当系统关闭或重新启动时会发生这种情况。
process.on('exit', function() {
// Shutdown logic
});
I've made a library that can be used to block and to detect the shutdown of an electron application: https://www.npmjs.com/package/@paymoapp/electron-shutdown-handler我制作了一个库,可用于阻止和检测 electron 应用程序的关闭: https://www.npmjs.com/package/@paymoapp/electron-shutdown-handler
It can be used like this:它可以这样使用:
import { app, BrowserWindow } from 'electron';
import ElectronShutdownHandler from '@paymoapp/electron-shutdown-handler';
app.whenReady().then(() => {
const win = new BrowserWindow({
width: 600,
height: 600
});
win.loadFile('index.html');
// you need to set the handle of the main window
ElectronShutdownHandler.setWindowHandle(win.getNativeWindowHandle());
// [optional] call this if you want to delay system shutdown.
// otherwise as soon as the onShutdown callback finishes, the system
// will proceed with shutdown and the app can be closed any time
ElectronShutdownHandler.blockShutdown('Please wait for some data to be saved');
Electron.ShutdownHandler.on('shutdown', () => {
// this callback is executed when the system is preparing to shut down (WM_QUERYENDSESSION)
// you can do anything here, BUT it should finish in less then 5 seconds
// if you call async stuff here, please take into account
// that if you don't block the shutdown (before the callback is fired)
// then the system will proceed with shutdown as soon as the function return
// (no await for async functions)
console.log('Shutting down!');
// if you need to call async functions, then do it like this:
yourAsyncSaveState().then(() => {
// now we can exit
// Release the shutdown block we set earlier
ElectronShutdownHandler.releaseShutdown();
// If you've blocked the shutdown, you should
// manually quit the app
app.quit();
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.