【问题标题】:Electron: Race condition between main and renderer processElectron:主进程和渲染器进程之间的竞争条件
【发布时间】:2020-12-14 05:19:55
【问题描述】:

在我的 Electron 应用程序中,我看到了奇怪的行为。在 Windows 中,有时渲染器进程会在 Electron 初始化完成之前执行,这会导致启动时出现问题。 例如:我在 Main.ts 文件的构造函数中设置了一个 sequelize 数据库并注册 IPC 通道,因此据我所知,app.on('ready') 事件应该在构造函数完成执行后触发,但有时在 Windows 操作系统中只是,即使在数据库设置之前,就绪事件也会触发,并且我的渲染器进程正在调用数据库以获取 MainWindow 的默认记录。

我认为这是渲染器进程和主进程执行之间的竞争条件,有人知道如何解决吗?

Main.ts

export class Main {
    private mainWindow: BrowserWindow;
    static instance: Main;
    public async init(ipcChannels: IpcChannelInterface[]) {
        Main.instance = this;

        // Registering the IPC Channels
        await this.registerIpcChannels(ipcChannels);

        var config = require('../../package.json');
        app.setAsDefaultProtocolClient(config.build.protocols.name);
        app.setAppUserModelId(config.build.appId);
        app.on('ready', Main.createWindow);
        app.on('window-all-closed', Main.onWindowAllClosed);
        app.on('activate', Main.onActivate);

        //Below statement setup the database
        await SequelizeDB.setup();

    }
}

(new Main()).init([new IpcChannel1(), new IpcChannel2()]);

【问题讨论】:

    标签: node.js typescript electron race-condition renderer


    【解决方案1】:

    只要 Electron 设置完成,ready 事件就会触发。它与您的构造函数或init 方法无关。来自docs

    在 Electron 完成初始化时发出一次

    听起来您是在说您的createWindow 函数依赖于数据库设置函数。在这种情况下,您可以先进行设置:

    await SequelizeDB.setup();
    await app.whenReady(); // this can replace your on("ready", ...) stuff
    Main.createWindow();
    

    【讨论】:

      猜你喜欢
      • 2017-06-01
      • 2021-12-04
      • 2018-01-05
      • 2020-04-09
      • 2017-05-01
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多