[英]Creating invisible window from main process to compute function in Electron
I am trying to write an Electron program in which the main process creates an invisible window, sends a number to that window, the window computes the factorial and then sends it back. 我正在尝试编写一个Electron程序,在该程序中主进程创建一个不可见的窗口,向该窗口发送一个数字,该窗口计算阶乘然后将其发送回去。
This is in the main process: 这是主要过程:
function invisibleWindow() {
const invisPath = 'file://' + path.join(__dirname, 'files/html/inv.html')
let win = new BrowserWindow({ width: 400, height: 400, show: false })
win.loadURL(invisPath)
win.webContents.on('did-finish-load', function () {
const input = 100;
win.webContents.send('compute-factorial', input);
})
ipcMain.on('factorial-computed', function (event, input, output) {
const message = `The factorial of ${input} is ${output}`
console.log(message);
})
}
The function gets called in the main process by: 该函数在主过程中通过以下方式调用:
app.on('ready', () => {
// creates different window here
invisibleWindow();
});
This is the inv.html file: 这是inv.html文件:
<html>
<script type="text/javascript">
const ipc = require('electron').ipcRenderer
const BrowserWindow = require('electron').remote.BrowserWindow
ipc.on('compute-factorial', function (event, number) {
const result = factorial(number)
ipcRenderer.send('factorial-computed', number, result)
window.close()
})
function factorial (num) {
if (num === 0) return 1
return num * factorial(num - 1)
}
</script>
</html>
Now after I have added this to my program, every time when I start it through the terminal, it does not terminate by itself even when all other (visible) windows are closed. 现在,将其添加到程序中后,每次通过终端启动它时,即使关闭了所有其他(可见)窗口,它也不会自行终止。 I guess this is because the invisible window is still opened because it did not receive the
compute-factorial
event. 我猜这是因为看不见的窗口仍处于打开状态,因为它没有收到
compute-factorial
事件。
What am I doing wrong? 我究竟做错了什么?
This is because of an race condition
. 这是因为
race condition
。 Electron docs: 电子文档:
Event: 'did-finish-load'
事件: “ did-finish-load”
Emitted when the navigation is done, ie the spinner of the tab has stopped spinning, and the onload event was dispatched.
导航完成时发出,即选项卡的微调器停止旋转,并调度onload事件。
You can try it with setTimeout
: 您可以使用
setTimeout
尝试一下:
win.webContents.on('did-finish-load', function () {
setTimeout(() => {
const input = 100;
win.webContents.send('compute-factorial', input);
}, 3000);
});
The main process does not know when the DOM is ready. 主进程不知道DOM准备就绪的时间。 You can do something like this.
你可以做这样的事情。
Send your main process an "dom-is-ready" event. 向您的主要流程发送“准备就绪”事件。
inv.html inv.html
ipc.send('dom-is-ready');
Paste your 'did-finish-load' code into 'dom-is-ready' . 将您的“完成加载”代码粘贴到“ dom-is-ready”中 。
main.js main.js
function invisibleWindow() {
const invisPath = 'file://' + path.join(__dirname, 'files/html/inv.html');
const win = new BrowserWindow({
width: 400,
height: 400,
show: false
});
win.loadURL(invisPath);
win.webContents.on('did-finish-load', function () {
win.show();
});
ipcMain.on('dom-is-ready', function (event) {
const input = 100;
win.webContents.send('compute-factorial', input);
});
ipcMain.on('factorial-computed', function (event, input, output) {
const message = `The factorial of ${input} is ${output}`
console.log(message);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.