Background download Thread Progress Windows problem


Well, instead of it being a modal dialog window, it could just be a component that could be added to a GUI or to the desktop…


My bad, I overlooked in the docs:

A thread that automatically pops up a modal dialog box with a progress bar

So launchThread will allow the message thread to run, but intentionally blocks user interaction.

The class to look at is probably ThreadPoolJob.

Just adding, have a look at this talk:

…just realised, that the page doesn’t show the name, it was given by Dave Rowland


Thank you all for help. I decided to create a new window with ProgressBar object. Is it a simple way to increment in loop through ProgressBar object to refresh progress variable without creating a new instance each time? I am looking for function similar to setProgress in ThreadProgressWindow.


ProgressBar takes in a reference to the floating point number that is used as the progress amount. Then you can just change the referred to number to get the progress bar to change. (Obviously that number has to be a member variable somewhere so that it sticks around and doesn’t cause a dangling reference in the ProgressBar.)


Hmm… I am incrementing progress value in a loop but it seems it passes only last value of progress. Most likely I am doing something wrong. Sample of code below:

class MainContentComponent : public URL,
							public Component,
							public Thread
	MainContentComponent() : Thread("Background Download Thread"), pb(progress)
	downloadButton.setButtonText("Select folder to download file from URL...");
	downloadButton.onClick = [this] { downloadButtonClicked(); };

	progress = 0.0;		

	setSize(300, 200);


void resized() override
	downloadButton.setBounds(10, 10, getWidth() - 20, 20);
	//Other buttons
	pb.setBounds(10, 130, getWidth() - 20, 20);
	void downloadButtonClicked()
		FileChooser chooser("Select path");

	if (chooser.browseForDirectory())

void run() override
	while (!threadShouldExit())
		URL fileUrl("");

		String filename = fileUrl.getFileName();
		ScopedPointer<InputStream> fileStream = fileUrl.createInputStream(false);

		// Reading file into buffer...

		FileOutputStream out(localfile);

		double j = mem.getSize();

		for (int i = 0; i < mem.getSize(); ++i)
			progress += i / j; // HERE PROGRESSBAR SHOULD MOVE



TextButton downloadButton;

ProgressBar pb;
double progress;

File chosenPath;




Have you checked your progress calculation ends up producing floating point numbers between 0.0 and 1.0?


I don’t see in your code, how a change in the progress variable would be either forwarded to a progressBar/Slider or a repaint is scheduled…


The ProgressBar has an internal timer that does the repaints. (And it takes the progress variable as a reference.)


I see, got it. Never used ProgressBar. I should read first :wink:


There’s also the really annoying possibility the compiler decides to optimize the progress calculation away, are you testing with a debug or release build?


In use with separate method i.e.

void ButtonClicked() {	progress += 0.1; }

progressBar is moving after each button click. When i try to loop this (from 0.0 to 1.0) it ends with 100%.


Maybe the code is just so fast the progress bar never ends up showing anything else but the 100%? (The repaint timer in the progress bar doesn’t run at super fast intervals if I recall right.)


I tested with Time::waitForMillisecondCounter(400); and same result. It seems that progress value is monitored only after it exits out of the range of function. I am wondering how it can be useful with such limitations.


Thread::sleep seems more appropriate in this situation


As a test (and only a test) try putting this in after you increment the progress value:

MessageManager::getInstance()->runDispatchLoopUntil (200);



The problem is you probably have not initialized ‘progress = 0’. Then you’re adding values to ‘progress’ in the run() but until ‘progress’ reaches 1% you will wait for eternity.

Edit: Ah, sorry, I see it’s in the constructor. I’ve got almost identical code in my apps and it works perfectly fine.

progress += i / j;

maybe you meant

progress = i / j;


Shouldn‘t i be converted to float first before dividing? Otherwise it will return 0 al the time. Or am I wrong here?

Edit: sorry, j is double so never mind :wink:


Personally I’d have written it with an explicit cast to double, just to make sure that’s clear to a reader, e.g.

progress = currentItem / (double) totalNumItems;


I think it was clear to reader, i was testing with hardcoded values also.
MessageManager::getInstance()->runDispatchLoopUntil (200);
works for testing in simple loop, but returns assertion when running in thread as it is not a Message thread.