从Electron.js的渲染进程中调用主文件main.js的函数以进行多线程处理。

huangapple go评论89阅读模式
英文:

Call of function from main.js in render process in electron js for multithreading

问题

我尝试在ElectronJS上创建多线程应用程序,但以前从未使用过,遇到了与线程相关的问题。我使用了threads.js(https://www.npmjs.com/package/threads)来实现多线程,而不是默认的Worker,当我调用sendFile()时,它可以在main.js中正常工作。我在main.js中有一个方法,它创建工作线程并从线程中获取结果。

sendFile = async (text) => {
    const send = await spawn(new Worker('./src/service/sender.js'));
    const result = await send(text);

    console.log(result);
    await Thread.terminate(send);
}

package.json

{
  "name": "XXXXX",
  "version": "1.0.0",
  "description": "XXXXXXX",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "author": "",
  "license": "ISC",
  "browser": {
    "fs": false
  },
  "dependencies": {
    "@electron/remote": "^2.0.9",
    "electron": "^24.3.1",
    "md5-file": "^5.0.0",
    "threads": "^1.7.0"
  }
}

如何在渲染块中使用这个方法?

英文:

I try to create multithreading application on ElectronJS but I've never been used it before and stack into problem with threads.
I used threads.js https://www.npmjs.com/package/threads for multithreading instead of default worker and it good work from main.js when I call sendFile()
I have method in main.js witch create worker and get result from thread.

sendFile = async (text) => {
    const send = await spawn(new Worker('./src/service/sender.js'));
    const result = await send(text);

    console.log(result);
    await Thread.terminate(send);
}

package.json

{
  "name": "XXXXX",
  "version": "1.0.0",
  "description": "XXXXXXX",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "author": "",
  "license": "ISC",
  "browser": {
    "fs": false
  },
  "dependencies": {
    "@electron/remote": "^2.0.9",
    "electron": "^24.3.1",
    "md5-file": "^5.0.0",
    "threads": "^1.7.0"
  }
}

How can I use this method in render block?

答案1

得分: 0

我采用以下方法,也许对某些人也有帮助。

我创建了一个方法,在主进程(main.js)中创建 Worker 并获取响应。

sendFile = async (text) => {
    const send = await spawn(new Worker('./src/service/sender.js'));
    const response = await send(text);
    await Thread.terminate(send);
}

在渲染进程(sender.js)中,我导入了 ipcRenderer 并使用 send() 方法向主进程发送信息。

const { ipcRenderer } = require('electron');
ipcRenderer.send('msg', 'Hello from render process');

在主进程(main.js)中,我创建了一个监听器:

const { ipcMain } = require('electron');
ipcMain.on('msg', (event, data) => {
    sendFile(data);
});
英文:

I use the following approach, maybe somebody find it helpful as well.

I created a method which creates Worker in the main process (main.js) and gets a response.

sendFile = async (text) => {
    const send = await spawn(new Worker('./src/service/sender.js'));
    const response = await send(text);
    await Thread.terminate(send);
}

In the render process (sender.js), I imported ipcRenderer and used send() method to send information to the main process.

const { ipcRenderer } = require('electron');
ipcRenderer.send('msg', 'Hello from render process');

and in the main process (main.js), I created a listener:

const { ipcMain } = require('electron');
ipcMain.on('msg', (event, data) => {
    sendFile(data);
});

huangapple
  • 本文由 发表于 2023年6月1日 01:28:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定