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 ipcMain
It 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!
曾经蜡笔没有小新2017-07-03 11:44:13
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')
})
巴扎黑2017-07-03 11:44:13
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.