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!
Found the solution ^_^
Listen to two events with the same name in
ipcMain
andipcRenderer
at the same time, and then usefocusedWindow.webContents.send('save-file')
in the main thread to trigger thesave-file
event ofipcRenderer
, just request thesave-file
event ofipcMain
in thesave-file
event ofipcRenderer
and carry the corresponding dataipcMain:
ipcRenderer:
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 thesave
function event and pass out the data you need.It’s a bit convoluted, but that’s all I can think of for now.