【发布时间】:2022-01-15 04:49:27
【问题描述】:
我目前在接收从我的主进程到我的渲染器进程(在 Angular 中)的活动窗口的恒定流时遇到一些问题。我知道接收每秒更新会破坏我的应用程序,所以我试图使用 setTimeout 来限制它。但这仍然行不通。我该如何解决这个问题?
渲染器 - Angular 中的组件
ipc: IpcRenderer | undefined | null;
constructor(private electronService: ElectronService) { }
ngAfterViewInit(): void {
this.getActiveWindow();
}
getActiveWindow() {
while(this.electronService.isElectronApp) {
this.ipc = this.electronService.ipcRenderer;
this.ipc.send('get-active-window');
this.ipc.on('get-active-window-reply', (_event, reply) => {
console.log(reply);
});
}
}
main.js
const activeWindows = require('electron-active-window');
ipcMain.on('get-active-window', (_event, _arg) => {
let i = 0;
setTimeout(function () {
activeWindows().getActiveWindow().then((result) => {
win.webContents.send("get-active-window-reply", result)
})
}, 10000 * i)
});
到目前为止,我已经尝试了以下方法,但这只会显示一次活动窗口。我想跟踪所有的变化:
渲染器 - Angular 中的组件
ngOnInit(): void {
this.getActiveWindow();
}
getActiveWindow() {
if(this.electronService.isElectronApp) {
this.ipc = this.electronService.ipcRenderer;
this.ipc.send('get-active-window');
this.ipc.on('get-active-window-reply', (_event, reply) => {
console.log(reply);
});
}
}
main.js
ipcMain.on('get-active-window', (_event, _arg) => {
activeWindows().getActiveWindow().then((result) => {
win.webContents.send("get-active-window-reply", result)
});
});
-
这段代码有很多问题。您有一个 while 循环,它不断发送事件和注册处理程序。响应处理程序只是发送延迟响应。双方在本质上都有些异步。想想你当前的代码会发生什么以及你想要什么,然后尝试从那里重新工作。
-
@Clashsoft 感谢您的回复。我已经更新了我的问题以显示我目前在哪里。第二次尝试似乎只获得一次窗口标题。我想让这个实时。我该如何解决这个问题?