GUI blocked on change of ComboBox selection

Hello,

maybe someone can help me with my problem here. I have a ComboBox list with items. I listen to changes like this:

theComboBox.onChange = [this] { 
    int itemId = theComboBox.getSelectedIdAsValue().getValue();
    loadData(itemId); // or Sleep(1000);
};

So far so good, everything works. My problem is, “loadData” does something that takes about one second, and the GUI is blocked until the loading has finished. As example you can replace “loadData” with “Sleep(1000)”.

GUI is blocked means, the ComboBox is like a dropdown and the user selects an item… The ComboBox of Juce automatically closes and displays the text of the selected item - you know. Now the problem is, it closes the box, but it does not change the text until everything in the “onChange” function has finished. I want to load that stuff “asynchronous”.

I’ve tried something like this, but it still blocks the GUI:

theComboBox.onChange = [this] { 
    auto asyncFn = [&]() { 
        int itemId = theComboBox.getSelectedIdAsValue().getValue();
        loadData(itemId); 
    }; 
    return std::async(std::launch::async, asyncFn);
};

The only way it did NOT block the GUI was putting a WindowAlert in between - and I have no idea why. So the following code does update the ComboBox widget first, then displays the alert, and THEN blocks the GUI but that’s actually OK because the user does not SEE that it takes 1 second until the GUI updates… but I don’t want to show an alert :smiley:

theComboBox.onChange = [this] { 
    AlertWindow::showMessageBox(AlertWindow::InfoIcon, "Test", "ABC", "OK"); 
    int itemId = theComboBox.getSelectedIdAsValue().getValue();
    loadData(itemId); // or Sleep(1000);
};

Any ideas?

Thanks in advance

Try

std::thread(asyncFn).detach();

instead of

std::async(std::launch::async, asyncFn);

as if the std::future obtained from std::async has temporary object lifetime (not moved or bound to a variable), the destructor of the std::future will block at the end of the full expression until the asynchronous operation completes

1 Like

Perfect! That saves my day, thank you!