Closed 07ke closed 5 months ago
Hi!
First of all: did you execute the setup step? (as shown here: https://github.com/paymoapp/electron-shutdown-handler#example, setting up the window handle with setWindowHandle
and calling blockShutdown
afterwards)
If you don't call blockShutdown
it can not be guaranteed that the async operations will be finished. Also note that blockShutdown must be called before the actual shutdown event (before the callback is called).
Also! Please refer to the demo. If you don't prevent the default behavior of before-quit
and close
events, then the electron app might just exit at a shutdown event, because even though preventShutdown
is called, windows tries to close all the applications in the event of a shutdown.
app.on('before-quit', e => {
e.preventDefault();
});
win.on('close', e => {
e.preventDefault();
});
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function writeFile(text) {
return new Promise((resolve, reject) => {
fs.writeFile('test.txt', text, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
})
})
}
ShutdownHandler.setWindowHandle(win.getNativeWindowHandle());
ShutdownHandler.blockShutdown('Yayy! It works! Shutdown in 10 seconds');
writeFile('!!!!!!!!!!!!!!!!!!!!!!!!!!!');
ShutdownHandler.on('shutdown', async () => {
await writeFile('! System is shutting down !');
await sleep(10000);
await writeFile('Shutting down NOW');
ShutdownHandler.releaseShutdown();
app.quit();
});
text.txt = ! System is shutting down !
"Shutting down NOW" No Write
I currently don't have a windows 11 system on hands, but I will look into this ASAP.
It seems that the handling of Promise type is missing.
Now it shuts down when the "shutdown quit 2" is not executed