Electron.IpcMain.On doesn't allow for event parameter #652

Closed
opened 2026-01-29 16:45:15 +00:00 by claunia · 1 comment
Owner

Originally created by @Zshandi on GitHub (Apr 14, 2021).

Originally assigned to: @GregorBiswanger on GitHub.

  • Version: 11.5.1
  • Target: Windows

Inside my main process, I am trying to receive a signal from my renderer process using IpcMain.On() and return a value back to the renderer process. The issue I am having is that I cannot access the event parameter inside of the callback function. I am receiving a compile time error stating that event is an "Invalid Expression term".

I looked at the source code for On() inside IpcMain.cs and it doesn't seem to be calling the listener callback function with an event parameter.
The comments say that the listener will be called as such:

///  Listens to channel, when a new message arrives listener would be called with 
///  listener(event, args...).

But looking at the implementation, it doesn't seem to be calling the listener with the event parameter as it says it does:

        public void On(string channel, Action<object> listener)
        {
            BridgeConnector.Socket.Emit("registerIpcMainChannel", channel);
            BridgeConnector.Socket.Off(channel);
            BridgeConnector.Socket.On(channel, (args) => 
            {
                List<object> objectArray = FormatArguments(args);

                if(objectArray.Count == 1)
                {
                    listener(objectArray.First());
                }
                else
                {
                    listener(objectArray);
                }
            });
        }

In particular, Action<object> listener forces the listener to only take a single argument, and listener(objectArray.First()); or listener(objectArray); only pass the argument object list, which doesn't include an event.

Steps to Reproduce:

  1. Use the following code in the main process:
Electron.IpcMain.On("signal-name", (event, args) => {
  // Do something(or not, either way it still gives the error)
});
Originally created by @Zshandi on GitHub (Apr 14, 2021). Originally assigned to: @GregorBiswanger on GitHub. <!-- Please search existing issues to avoid creating duplicates. --> <!-- Which version of Electron.NET CLI and API are you using? --> <!-- Please always try to use latest version before report. --> * **Version**: 11.5.1 <!-- Which version of .NET Core and Node.js are you using (if applicable)? --> <!-- What target are you building for? --> * **Target**: Windows <!-- Enter your issue details below this comment. --> <!-- If you want, you can donate to increase issue priority (https://donorbox.org/electron-net) --> Inside my main process, I am trying to receive a signal from my renderer process using `IpcMain.On()` and return a value back to the renderer process. The issue I am having is that I cannot access the event parameter inside of the callback function. I am receiving a compile time error stating that event is an "Invalid Expression term". I looked at the source code for `On()` inside `IpcMain.cs` and it doesn't seem to be calling the listener callback function with an event parameter. The comments say that the listener will be called as such: ``` /// Listens to channel, when a new message arrives listener would be called with /// listener(event, args...). ``` But looking at the implementation, it doesn't seem to be calling the listener with the event parameter as it says it does: ``` public void On(string channel, Action<object> listener) { BridgeConnector.Socket.Emit("registerIpcMainChannel", channel); BridgeConnector.Socket.Off(channel); BridgeConnector.Socket.On(channel, (args) => { List<object> objectArray = FormatArguments(args); if(objectArray.Count == 1) { listener(objectArray.First()); } else { listener(objectArray); } }); } ``` In particular, `Action<object> listener` forces the listener to only take a single argument, and `listener(objectArray.First());` or `listener(objectArray);` only pass the argument object list, which doesn't include an event. Steps to Reproduce: 1. Use the following code in the main process: ``` Electron.IpcMain.On("signal-name", (event, args) => { // Do something(or not, either way it still gives the error) }); ```
claunia added the bug label 2026-01-29 16:45:15 +00:00
Author
Owner

@GregorBiswanger commented on GitHub (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!

@GregorBiswanger commented on GitHub (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!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#652