Add EventArgs to BrowserWindow.OnClose event to prevent closing the window #880

Closed
opened 2026-01-29 16:51:05 +00:00 by claunia · 4 comments
Owner

Originally created by @niklasstich on GitHub (Apr 21, 2023).

Currently, the OnClose event on BrowserWindow does not provide any event args to callees of the event. This makes it impossible to prevent closing the window from C# code directly without involving Javascript that catches the 'onbeforeunload' event.

This fact was mentioned in several previous issues:
https://github.com/ElectronNET/Electron.NET/issues/391#issuecomment-719928364
https://github.com/ElectronNET/Electron.NET/issues/567

I would therefore request that EventArgs be added to this event that allow preventing the window from being closed, analogous to the Electron BrowserWindow close event: https://www.electronjs.org/docs/latest/api/browser-window#event-close

Originally created by @niklasstich on GitHub (Apr 21, 2023). Currently, the `OnClose` event on `BrowserWindow` does not provide any event args to callees of the event. This makes it impossible to prevent closing the window from C# code directly without involving Javascript that catches the 'onbeforeunload' event. This fact was mentioned in several previous issues: https://github.com/ElectronNET/Electron.NET/issues/391#issuecomment-719928364 https://github.com/ElectronNET/Electron.NET/issues/567 I would therefore request that EventArgs be added to this event that allow preventing the window from being closed, analogous to the Electron BrowserWindow `close` event: https://www.electronjs.org/docs/latest/api/browser-window#event-close
claunia added the Feature label 2026-01-29 16:51:05 +00:00
Author
Owner

@ssrdop commented on GitHub (Jun 5, 2023):

Hello! OnClose event still doesnot has event args to prevent closing. I know how to prevent App closing, but browserWindow still doesnot has this feature.
At https://github.com/ElectronNET/Electron.NET/issues/567#issuecomment-1487165152 u said "With native Electron 23 and .NET 6 support. Your problem should be fixed here. " but seems it doesnot. Can u explain, pls?

@ssrdop commented on GitHub (Jun 5, 2023): Hello! OnClose event still doesnot has event args to prevent closing. I know how to prevent App closing, but browserWindow still doesnot has this feature. At https://github.com/ElectronNET/Electron.NET/issues/567#issuecomment-1487165152 u said "With native Electron 23 and .NET 6 support. Your problem should be fixed here. " but seems it doesnot. Can u explain, pls?
Author
Owner

@terryjbutler commented on GitHub (Jun 19, 2023):

Good day! I've also been facing this problem. Any information on this would be very appreciated.

@terryjbutler commented on GitHub (Jun 19, 2023): Good day! I've also been facing this problem. Any information on this would be very appreciated.
Author
Owner

@ssrdop commented on GitHub (Jun 20, 2023):

Good day! I've also been facing this problem. Any information on this would be very appreciated.

Hey ive found some solution - u can use window.onbeforeunload

so when ur app starts u can add ur callback (even async) to window.onbeforeunload

I have react app, so i wrote something like this:

useEffect(() => {
        window.onbeforeunload = async () => {
            await beforeApplicationClosed()
            if (!canCloseApp)
                return
        }
}, [])

So u can make it with any javasript framework, react just for example

It prevents closing app and it always calls ur callback function u wrote inside window.onbeforeunload.

But there are some problem - even if u want to close app now - u cant.

But for this problem i also have a solution - if u know that u want to close ur app then do next:
window.onbeforeunload = null

and call after i:
Electron.App.Quit(); // inside ur back, using websockets or via http api, doesnot matter.

How it works - when ur app starts u assing to window.onbeforeunload some callback thats calling everytime ur application or window is closing and its prevents closing at all (event if u press alt+f4, or from window toolbar). So often u assign this callback for window.onbeforeunload when u want to save some data before or ask user confirmation or something else. If u did this job and u ready to close app - just doint "window.onbeforeunload = null" resets any callback on closing and u need to call "Electron.App.Quit();" and after it there are no callback for window.onbeforeunload and ur app closing.

Sorry i am not native english speaker, but i hope u will understand.

@ssrdop commented on GitHub (Jun 20, 2023): > Good day! I've also been facing this problem. Any information on this would be very appreciated. Hey ive found some solution - u can use window.onbeforeunload so when ur app starts u can add ur callback (even async) to window.onbeforeunload I have react app, so i wrote something like this: useEffect(() => { window.onbeforeunload = async () => { await beforeApplicationClosed() if (!canCloseApp) return } }, []) So u can make it with any javasript framework, react just for example It prevents closing app and it always calls ur callback function u wrote inside window.onbeforeunload. But there are some problem - even if u want to close app now - u cant. But for this problem i also have a solution - if u know that u want to close ur app then do next: window.onbeforeunload = null and call after i: Electron.App.Quit(); // inside ur back, using websockets or via http api, doesnot matter. How it works - when ur app starts u assing to window.onbeforeunload some callback thats calling everytime ur application or window is closing and its prevents closing at all (event if u press alt+f4, or from window toolbar). So often u assign this callback for window.onbeforeunload when u want to save some data before or ask user confirmation or something else. If u did this job and u ready to close app - just doint "window.onbeforeunload = null" resets any callback on closing and u need to call "Electron.App.Quit();" and after it there are no callback for window.onbeforeunload and ur app closing. Sorry i am not native english speaker, but i hope u will understand.
Author
Owner

@FlorianRappl commented on GitHub (Nov 1, 2025):

This is not possible as the preventDefault is a method, i.e., it cannot be serialized. Even if it could (or we would allow calling it indirectly) it would not be possible - the bridge is async and therefore the event has been fully processed once the method would actually be called.

@FlorianRappl commented on GitHub (Nov 1, 2025): This is not possible as the `preventDefault` is a method, i.e., it cannot be serialized. Even if it could (or we would allow calling it indirectly) it would not be possible - the bridge is async and therefore the event has been fully processed once the method would actually be called.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#880