10-19-2012 09:40 AM
I need some help understanding how to update a progress bar.
I have an operation that runs in a loop that takes a long time to complete. Once every loop, I update the progressBar.progress. However, it doesn't update until the entire operation is completed. I tried using setTimeout to cause a pause but that didn't work either. Any ideas?
Pseudo code:
for (i=0;i++;i<numstufftodo)
{
progressBar.progress = i/numstufftodo;
setTimeout(processStuff(),1);
}
function processStuff():void
{
// do complex calcs here
}
10-19-2012 10:49 AM
It might be because the UI thread isn't being able to update until it exits the loop (Air is single threaded).
You can try putting your progress bar update in a function or having your processStuff() send out events you can capture to have it update. I know the events would work, I am guessing on the function one. I am away from my computer and unable to test it for you ![]()
Maybe someone else will have a better way ![]()
10-19-2012 06:35 PM
Although your pseudocode looks like it should update the progress property as you intend, have you tried tracing out its value for each iteration through your loop, to confirm that the integer division on the right hand side is getting upgraded to a Number properly, and that your loop is not assigning a value of zero to the progress property for all of your loop iterations prior to the last iteration?
10-19-2012 09:32 PM
10-19-2012 09:37 PM
So does the task just run so fast that the user doesn't really need to see a progress bar? Could there be cases where it will run a lot slower than it has done in your testing so far?
10-20-2012 06:18 AM
The task runs for several seconds during which the UI freezes.
Here is what I think is happening:
1) For loop runs, calling the task function many times as it loops through, and also updating the progress as it goes. However, UI does not update during this time.
2) For loop ends, but the task functions are still queued to run, meanwhile, progress is already set to 1 because loop is done, but UI is still not updated.
3) Now the task functions that were called are executed one by one, meanwhile the UI is frozen.
4) The tasks complete, and the UI is finally updated, the progress bar jumps to full.
I think what I need is some sort of event driven way to not execute the next iteration of the loop until the task function called during that loop is finished executing.
10-20-2012 08:50 AM
Maybe you could post your code (or ideally a very stripped down version of it that still demonstrates the problem)?
10-20-2012 09:20 AM
BTW, one other thing: in your pseudocode, your setTimeout() call is doing a function invocation (of the "processStuff" function), *not* passing the function to setTimeout() as a closure, as it is supposed to:
" Do not include quotation marks or parentheses, and do not specify parameters of the function to call. For example, use functionName, not functionName() orfunctionName(param)."
If you are including the call operator (i.e. the parens) after your function name, you are actually passing the return value of your processStuff() function to setTimeout() as a function name, which is highly unlikely to be what you intend.