[英]Is there any way in electron to detect a shutdown on Windows?
我嘗試了“會話結束”、“窗口全部關閉”來捕獲 windows 關閉事件。 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();
}
});
使用節點的進程退出事件在進程退出時運行代碼,當系統關閉或重新啟動時會發生這種情況。
process.on('exit', function() {
// Shutdown logic
});
我制作了一個庫,可用於阻止和檢測 electron 應用程序的關閉: https://www.npmjs.com/package/@paymoapp/electron-shutdown-handler
它可以這樣使用:
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.