【发布时间】: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