node.js - Problems with the communication between the electron main thread and the rendering thread (how the Menubar event obtains data from the rendering thread)
大家讲道理
大家讲道理 2017-07-03 11:42:13
0
2
1277

Hello everyone:

I am using electron to write an application similar to an editor. There is a file saving function in the application menu bar:

Because the menu bar is in the main thread, but the save operation needs to obtain the content in an editor in the rendering thread. The official website only has examples of the rendering thread requesting the main thread (ipcMain and ipcRenderer), but ipcMainIt seems that it cannot actively request ipcRenderer.

So I would like to ask everyone, how does the main thread actively request the rendering thread to call the rendering thread's method or trigger the rendering thread's event?

Thanks!

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
曾经蜡笔没有小新

Found the solution ^_^

Listen to two events with the same name in ipcMain and ipcRenderer at the same time, and then use focusedWindow.webContents.send('save-file') in the main thread to trigger the save-file event of ipcRenderer , just request the save-file event of ipcMain in the save-file event of ipcRenderer and carry the corresponding data

ipcMain:

ipcMain.on('save-file' ,(event ,arg) => {
    console.log(arg)
})

ipcRenderer:

ipcRenderer.on('save-file' ,(event ,arg) => {
  ipcRenderer.send('save-file' ,'test')
})
巴扎黑

You can first bind an event to the rendering thread, such as ipcRenderer.on('save', save);, then trigger this event when the user clicks Save in the menu, and then broadcast it in the save function event and pass out the data you need.

It’s a bit convoluted, but that’s all I can think of for now.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template