View Single Post
Old 03-19-2017, 02:26 PM   #1
SpiderFox
Human being with feelings
 
Join Date: Jan 2017
Posts: 29
Default JUCE GUI hangs when executing std::future

I want to fetch data for a GUI.

I do this by calling std::future<std::vector<Project>> f = std::async(std::launch::async, &ProjectsController::getProjects, &pc) in the component which is intended to show the data.

Code:
ProjectBrowserComponent::ProjectBrowserComponent() : m_btn("OK") {
	addAndMakeVisible(m_tabs);
	setSize(500, 600);
	initData();
}

void ProjectBrowserComponent::initData() {
	std::cout << "requesting projs";
	std::future<std::vector<Project>> f = std::async(std::launch::async, &ProjectsController::getProjects, &pc);
	std::cout << "requested projs";
	std::vector<Project> projs;
	if (isReady(f)) {
		projs = f.get();
		std::cout << "got projs";
	}
}

template<typename R>
bool isReady(std::future<R> const& f)
{
	return f.wait_for(std::chrono::seconds(-1)) == std::future_status::ready;
}
The data is provided by a method called ProjectsController::getProjects() which lets it's thread sleep to simulate a remote service call which takes some time.

Code:
std::vector<Project> ProjectsController::getProjects() {
	std::vector<Project> result;
	if(serviceClient != nullptr) {
		try
		{
			std::cout << "controller requesting projs\n";
			std::this_thread::sleep_for(std::chrono::seconds(5));
			std::cout << "controller requested projs\n";
			result = serviceClient->getAvailableProjects();
		}
		catch (const std::exception&)
		{

		}
	}
	
	return result;
}
However, the UI does only apper after the thread sleep time has elapsed.

How can I make it load in the background?

If you want to have a look at it you can setup the project as follows:

Source code https://github.com/CodingSpiderFox/ReaperDAWHub

Install the conan package manager as python package
Code:
pip install conan
Install the dependencies

Code:
conan install --build Boost --build zlib --build Catch --build rapidjson -s arch=x86 -s build_type=Release -s compiler="Visual Studio" -s compiler.runtime=MD -s compiler.version=14
The project can be built with Visual Studio 2015 in the Release|Win32 configuration. (For lazyness, the project is built to C:\Program Files (x86)\REAPER\Plugins) so make sure to start VS 2015 as administrator or change the output directories

Last edited by SpiderFox; 03-19-2017 at 02:37 PM.
SpiderFox is offline   Reply With Quote