paymoapp / electron-shutdown-handler

Handle shutdown messages on windows in your electron app and delay system shutdown
MIT License
22 stars 8 forks source link

Windows 11 can't stop #3

Closed 07ke closed 5 months ago

07ke commented 1 year ago
ElectronShutdownHandler.on('shutdown', async () => {
      logger.warn('shutdown quit 1')
     await sleep(3000) // = wind reg ProxyEnable
      logger.warn('shutdown quit 2')
      ElectronShutdownHandler.releaseShutdown()
      app.quit()
})

Now it shuts down when the "shutdown quit 2" is not executed

gergof commented 1 year 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).

gergof commented 1 year ago

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.

07ke commented 1 year ago

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

gergof commented 1 year ago

I currently don't have a windows 11 system on hands, but I will look into this ASAP.

ACCia commented 1 year ago

It seems that the handling of Promise type is missing.