question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IpcMain : add support for async callbacks

See original GitHub issue

Callbacks 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:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
marcianobandeiracommented, Feb 15, 2021

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?

2reactions
GregorBiswangercommented, Mar 28, 2023

🎉🚀 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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found