electron 电子物体被摧毁

9vw9lbht  于 9个月前  发布在  Electron
关注(0)|答案(1)|浏览(119)

当我关闭我的电子应用程序,有时我得到一个错误对象销毁blabla。我在谷歌上搜索了一下,我真的不认为这是一个正确的方法来解决它...有人建议这样修

ipcMain.on('aMessage', async e => {
    
    // do smth with a message and then send the answer

    //next is the fix... really?
    try {
        win.webContents.send('position', res.data);
    } catch (error) {
        console.log(error);
    }
});

所以建议的修复方法只是将发送行 Package 到try catch块中…它只是不能像它的意思...请告诉如何正确修复对象破坏电子错误。谢谢你

iovurdzv

iovurdzv1#

你的代码似乎有一个竞争条件,在你发送“位置”IPC之前,主窗口已经被破坏了。假设您正在获取TypeError: Object has been destroyed,您可以在发送IPC之前检查窗口是否仍然存在。举例来说:

ipcMain.on('aMessage', async e => {
  if (!win.isDestroyed()) {
    win.webContents.send('position', res.data);
  }
});

相关问题