如何防止(延迟)OS(Windows 10)关闭Electron窗口?

如何防止(延迟)OS(Windows 10)关闭Electron窗口?

问题描述:

我已经使用electronic + node.js制作了一个桌面应用程序.有时Windows会执行自动更新并重新启动OS.我想防止Windows 10重新启动,直到在软件中保存数据(数据库处于联机状态,因此需要一些时间来存储数据)为止.

I have made a desktop application using electron + node.js. Sometimes Windows does automatic updates and restarts the OS. I want to prevent Windows 10 from restarting until the data is saved (database is online so it takes some time to store data) in software.

现在,我正在使用以下代码来防止窗口关闭.保存数据后,我调用ipcMain.on('',function())方法并将lockwindow设置为true,然后我调用窗口关闭方法.它通常在正常关闭窗口或使用快捷键关闭窗口时起作用.但是,如果强制关闭或学习/重新启动,则不会发出此事件

Right now, I am using the below code to prevent the window from closing. After data save I am calling ipcMain.on('',function()) method and make lockwindow to true then i am calling window close method. It is working when normally window close or use shortcut keys for a close window. But this event is not emitted in case of force close or studown/restart

mainWindow.on('close', event => {
        if (lockWindow) {
            mainWindow.webContents.send('save', '');
            mainWindow.webContents.once('dom-ready', () => {
                mainWindow.webContents.send('save', '');
            });
            event.preventDefault();
            createdialogWindow();
        } else
            mainWindow = null
    })

谢谢.

看看窗口事件,特别是 关闭之前卸载 .

Have a look at window events, specifically close and beforeunload.

您将只有有限的时间才能使系统重新启动.

You will have only limited time before the system restarts itself anyway.

如果操作系统或用户决定终止您的应用程序,那么无论如何都会发生这种情况(您也可能因不擅长游戏而生气/激怒用户).

If the OS or the user decides to kill your app, this is what is going to happen anyway (you can also upset / anger user for not playing nicely).

最后,您是否希望您的应用程序成为未安装某些关键安全更新的原因?

Lastly, would you like your application to be the reason why some crucial security updates did not install?

HTH