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.

Electron App events not firing

See original GitHub issue

I’ve tried subscribing to BeforeQuit, Quitting, WillQuit, and WindowAllClosed. Regardless of the event, my code never fires when I close the Electron window.

            Electron.App.BeforeQuit += SaveOptions;
            Electron.App.Quitting += SaveOptions;
            Electron.App.WillQuit += SaveOptions;
            Electron.App.WindowAllClosed += SaveOptions;

Using version 0.0.7 on Windows 10.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
GregorBiswangercommented, Nov 10, 2017

I coded the night long for you and fix it and implement one more feature: Prevent Quiting 😉 I synced my work on master. The next Electron.NET API Version 0.0.8 have the fix. I will write a short documentation here tomorrow. I´m tired now.

Enjoy!

0reactions
GregorBiswangercommented, Nov 10, 2017

You can execute your logic before the application will exit now:

public void ElectronBootstrap()
{
    // Emitted before the application starts closing its windows. 
    Electron.App.BeforeQuit += App_BeforeQuit;

    // Emitted when all windows have been closed and the application will quit. 
    Electron.App.WillQuit += App_WillQuit;

    // Emitted when the application is quitting.
    Electron.App.Quitting += App_Quitting;
}

private async Task App_BeforeQuit(QuitEventArgs arg)
{
    await Electron.Dialog.ShowMessageBoxAsync("Before Quit");
    await Electron.Dialog.ShowMessageBoxAsync("Before Quit 2");
}

private async Task App_WillQuit(QuitEventArgs arg)
{
    await Electron.Dialog.ShowMessageBoxAsync("Will Quit");
    await Electron.Dialog.ShowMessageBoxAsync("Will Quit 2");
}

private async Task App_Quitting()
{
    await Electron.Dialog.ShowMessageBoxAsync("Quitting");
    await Electron.Dialog.ShowMessageBoxAsync("Quitting 2");
}

Calling args.PreventDefault() will prevent the default behaviour, which is terminating the application:

private async Task App_BeforeQuit(QuitEventArgs arg)
{
    arg.PreventDefault();

    await Electron.Dialog.ShowMessageBoxAsync("Before Quit");
    await Electron.Dialog.ShowMessageBoxAsync("Before Quit 2");
}

The default behavior in Electron.NET is to quit the app on window all closed. If the user pressed Cmd + Q, or the developer called Electron.App.Quit(), Electron will first try to close all the windows and then emit the Electron.App.WillQuit event, and in this case the Electron.App.WindowAllClosed event would not be emitted. To deactivate the default behavior, set the Electron.WindowManager.IsQuitOnWindowAllClosed to false. The Electron.App.WindowAllClosed event would work.

public void ElectronBootstrap()
{
    Electron.WindowManager.IsQuitOnWindowAllClosed = false;

    // Emitted when all windows have been closed.
    Electron.App.WindowAllClosed += App_WindowAllClosed;
}

private async void App_WindowAllClosed()
{
    await Electron.Dialog.ShowMessageBoxAsync("All windows closed");
}

Enjoy!

Read more comments on GitHub >

github_iconTop Results From Across the Web

why is my eventlistener not working in electron
I am trying to learn Electron js. I have a very simple HTML in my index.html <!doctyp html> ... all the other normal...
Read more >
ready-to-show event is not fired and app window doesn't ...
It's likely that this bug also affect Electron 9.x. My app is using Electron 9.3.0 and a few users have reported that the...
Read more >
app | Electron
Emitted when all windows have been closed. If you do not subscribe to this event and all windows are closed, the default behavior...
Read more >
How to get drop events working in Electron
Adding drag and drop for files to your Electron app should be easy ... You register a drop event listener and things are...
Read more >
How to Identify and Troubleshoot Issues in Your Electron App
When developing Electron apps it can be tough to identify and troubleshoot issues. To help out with this, today we're going to take...
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