简体   繁体   English

如何防止(延迟)操作系统(Windows 10)关闭电子窗口?

[英]How to prevent(delay) OS(Windows 10) from closing Electron window?

I have made a desktop application using electron + node.js.我使用电子 + node.js 制作了一个桌面应用程序。 Sometimes Windows does automatic updates and restarts the OS.有时,Windows 会自动更新并重新启动操作系统。 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.我想阻止 Windows 10 在软件中保存数据之前重新启动(数据库处于联机状态,因此存储数据需要一些时间)。

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.数据保存后,我调用 ipcMain.on('',function()) 方法并使 lockwindow 为 true 然后我调用窗口关闭方法。 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
    })

Thank You.谢谢你。

Have a look at window events, specifically close and beforeunload .看看窗口事件,特别是closebeforeunload

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 HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM