Hi,
I am developing a DSP configuration dialog and I need to call "playback_control::playback_format_title" inside there. Because you can't do this outside the main thread, I have created a main_thread_callback object where I do this call.
Now I have difficulties accessing the result of "playback_format_title" in my configuration dialog again. At the moment, I use std::future and std::promise for this. But this does not work. When click the button that invokes the call and waits for the result, foobar2000 freezes. It doesn't crash, but the windows does not respond and I get a "foobar2000 does not respond anymore." message from Windows.
Here is my code:
#include <future>
struct playback_format_title_callback : main_thread_callback
{
public:
virtual void callback_run() {
titleformat_object::ptr script;
static_api_ptr_t<titleformat_compiler>()->compile_safe_ex(script, pattern);
static_api_ptr_t<playback_control> playback_control;
pfc::string8 returnVal;
if (playback_control->playback_format_title(NULL, returnVal, script, NULL, playback_control::display_level_all)) {
promise.set_value(returnVal);
} else {
promise.set_value("");
}
}
void setPattern(const pfc::string8& pat) {
pattern = pat;
}
std::future<pfc::string8> getFuture() {
return promise.get_future();
}
private:
std::promise<pfc::string8> promise;
pfc::string8 pattern;
};
class CMyDSPPopup : public CDialogImpl < CMyDSPPopup > {
// ...
void onButtonClick() {
pfc::string8 pattern = get_pattern();
service_ptr_t<playback_format_title_callback> cb = new service_impl_t<playback_format_title_callback>();
cb->setPattern(pattern);
std::future<pfc::string8> future = cb->getFuture();
static_api_ptr_t<main_thread_callback_manager> cb_manager;
cb_manager->add_callback(cb);
pfc::string8 returnVal = future.get(); // <-- At this point, foobar freezes
do_something(returnVal);
}
// ...
}
static void RunDSPConfigPopup(const dsp_preset & p_data, HWND p_parent, dsp_preset_edit_callback & p_callback) {
CMyDSPPopup popup(p_data, p_callback);
if (popup.DoModal(p_parent) != IDOK) p_callback.on_preset_changed(p_data);
}
I already double checked: promise.set_value() is definitively executed.
Any ideas?
SDK 2015-01-15 / Visual Studio 2013 Update 4 / Whole Program Optimization turned off.