Message loop is not exited after stopDispatchLoop until tray icon is clicked again

I am trying to add a tray icon with a popup menu that lets the user close the app, but for some reason the app does not close until the main window or the tray icon is clicked a second time after quit.

I can see that the call to the below method is being made when I choose to quit the app:

void JUCEApplicationBase::quit()
{
    MessageManager::getInstance()->stopDispatchLoop();
}

…but the last line in main is not reached until the tray icon is clicked again:

int JUCEApplicationBase::main()
{
    ScopedJuceInitialiser_GUI libraryInitialiser;
    jassert (createInstance != nullptr);

    const std::unique_ptr<JUCEApplicationBase> app (createInstance());
    jassert (app != nullptr);

    if (! app->initialiseApp())
        return app->shutdownApp();

    JUCE_TRY
    {
        // loop until a quit message is received..
        MessageManager::getInstance()->runDispatchLoop();
    }
    JUCE_CATCH_EXCEPTION

    return app->shutdownApp();
}

This used to work before, and seem to have been introduced in https://github.com/WeAreROLI/JUCE/commit/7c45ad695c9d493b86b969e691dfa63e7e76d52d

Minimal example: https://github.com/abrykt/tray_test

Friendly bump :slight_smile:

One way to fix this is to mirror what the DemoRunner does and show the menu asynchronously:

This avoids some problematic modal state.

1 Like

Hello guys, Is it possible to extend this behaviour also when clicking a “Quit” option of the MenuBarModel?
Until I click again on the tray Icon nothing happens.

Thanks,
Davide