IpcMain : add support for async callbacks
See original GitHub issueCallbacks triggered by the UI are async (with respect to the web UI) but are handled with a non-async C# action:
Electron.IpcMain.On(string channel, Action<object> listener)
It is now common for C# controller methods to be async so it would be convenient to support them directly with the following overload i.e.
Electron.IpcMain.On(string channel, Func<object, Task> listener)
Bridging the sync -> async boundary, might just mean using Task.Run
but I don’t know whether running it on the threadpool is actually optimal because I assume On
is already being invoked on it’s own thread. By implementing this API, hopefully you are able to make the right choice about how to execute the async method without adding unnecessary overhead.
//
// Is invoking on the threadpool ideal?
//
public void On(string channel, Func<object, Task> listener) =>
this.On(
channel,
args => Task.Run(async () => await listener(args)));
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
How to add a callback to ipc renderer send
renderer.js import { ipcRenderer } from 'electron'; (async () => { const result = await ipcRenderer. invoke('an-action', [1, 2, 3]); console. ...
Read more >ipcMain
The ipcMain module is an Event Emitter. When used in the main process, it handles asynchronous and synchronous messages sent from a renderer...
Read more >Inter-Process Communication
In Electron, processes communicate by passing messages through developer-defined "channels" with the ipcMain and ipcRenderer modules.
Read more >Electron - 3 Methods for Inter Process Communications (IPC)
The “on” method can be used both for synchronous and asynchronous communication depending on how you use the properties of the callback function ......
Read more >ipcMain · Electron documentation
Communicate asynchronously from the main process to renderer processes. ... The ipcMain module is an instance of the EventEmitter class. When used in...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
currently, electron have handle and invoke methods, wich provides async await calls
// Renderer process ipcRenderer.invoke(‘some-name’, someArgument).then((result) => { // … })
// Main process ipcMain.handle(‘some-name’, async (event, someArgument) => { const result = await doSomeWork(someArgument) return result })
it’s possible in Electron.NET?
🎉🚀 New Electron.NET version 23.6.1 released 🚀🎉
With native Electron 23 and .NET 6 support. Your problem should be fixed here. If you continue to have the problem, please let us know. Please note the correct updating of your API & CLI. Info in the README. Have fun!