如何判断窗口是否在电子中关闭失败?



Electron有没有办法判断窗口是否没有成功关闭?

win.once("closed", () => {
// send message to the page running in the renderer process that the window was closed
});
win.close();

假设我没有在closebeforeunload处理程序中取消关闭,窗口是否仍无法关闭,或者我是否可以确定消息将始终发送到来宾页面?

我也刚刚遇到了这个问题,我所做的只是等待几百毫秒,如果调用回调,那么很可能,这个窗口无法关闭:

win.on('close', () => setTimeout(() => console.log('failed to close'), 300))

看看文档中的此属性:

win.closed

在子窗口关闭后设置为 true 的布尔值。

还有一点:

win.destroy()

强制关闭窗口,卸载和卸载前 不会为网页发出事件,关闭事件也不会 为此窗口发出,但它保证关闭事件将是 排放。

有了这个,您应该拥有创建确保窗口关闭的功能所需的所有信息:

function forceClose(window) {

// try close first
window.close()

// force with destroy
if(!window.closed) {
window.destroy()
}

//just logging out the event
window.on('closed', (e) => {
console.log(e)
})

}
// in your code, instead of calling win.close()
forceClose(win)

最新更新