IpcMain : add support for async callbacks #471

Open
opened 2026-01-29 16:40:37 +00:00 by claunia · 0 comments
Owner

Originally created by @duncanawoods on GitHub (Mar 31, 2020).

Originally assigned to: @GregorBiswanger on GitHub.

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)));
Originally created by @duncanawoods on GitHub (Mar 31, 2020). Originally assigned to: @GregorBiswanger on GitHub. Callbacks triggered by the UI are async (with respect to the web UI) but are handled with a non-async C# action: ``` csharp 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. ``` csharp 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. ``` csharp // // 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))); ```
claunia added the help wantedFeature labels 2026-01-29 16:40:37 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#471