0

Would anyone know a method (or trick) to force a rendering update to an MX ProgressBar in manual mode when using setProgress?

I have a situation with a block of code containing a couple of for loops which take a bit of time to complete. It would be tedious to unwrap this code to generate events, etc.

Update

Let me expand on this with a bit of pseudo code. I want to update the progress bar during operations on the contents of an array. THe for loops blocks so the screen isn't updating. I've tried validateNow() but that had no effect.

Is there some non-convoluted way I can either unwrap the for loop or use AS3's event model to update a progress bar? (I'm more accustomed to multi-threaded environments where this sort of task is trivial).

   private function doSomeWork():void {
       progressBar.visible = true;

       for(var n = 0; n < myArray.length; n++){

            progressBar.setProgress(n, myArray.length);
            progressBar.label = "Hello World " + n;
            progressBar.validateNow(); // this has no apparent effect

            var ba:ByteArray = someDummyFunction(myArray[i]);
            someOtherFunction(ba);
        }

         progressBar.visible = false;
      }
spring
  • 18,009
  • 15
  • 80
  • 160
  • 1
    Have you tried [validateNow()](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/core/UIComponent.html#validateNow()) ? Per description, it should redraw the component. –  May 12 '15 at 12:16
  • Hmm, it says it should be updated when you call `setProgress()` if the progress bar's control mode is `manual`. Probably you have cached its parent as bitmap? Or the mode got reset? – Vesper May 12 '15 at 12:16
  • @DodgerThud - no joy. Have tried calling `progressBar.validateNow();` and `validateNow()` within the `ProgressBar` parent after calling `progressBar.setProgress(n1, n2)` and setting the `label` text but it never gets updated. Odd. Have encountered this before. – spring May 12 '15 at 12:37
  • is your loop blocking the UI so no UI events can be handled? if yes you may need to split the long running job in smaller chunks – simion314 May 12 '15 at 13:28
  • Check if this helps: http://stackoverflow.com/questions/1120822/forcing-flex-to-update-the-screen – akhil_mittal May 13 '15 at 01:45

2 Answers2

1

In Flex, the screen is never updating while Actionscript code is running. It basically works like this:

  1. Execute all runnable Actionscript code.
  2. Update the screen.
  3. Repeat continuously.

To learn more details, google for [flex elastic racetrack]. But the above is the nut of what you need to understand.

If you don't want a long-running piece of code to freeze the screen, you'll have to break it up into chunks and execute them across multiple frames, perhaps within a FRAME_ENTER event handler.

Dave 114
  • 183
  • 2
  • 2
  • 12
  • Or use an [Actionscript Worker](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/Worker.html) – Brian May 14 '15 at 21:44
0

I am not sure what exactly is the problem. I tried the following code and it works without any need to validateNow.

  protected function button2_clickHandler(event:MouseEvent):void
  {
        for(var n:int = 0; n < 100; n = n+20){
        progressBar.setProgress(n, 100);
        progressBar.label = "Hello World " + n;
        // progressBar.validateNow(); 
        }
  }
  <mx:VBox width="100%" height="100%">
        <mx:ProgressBar id="progressBar"/>
        <mx:Button label="Update Progress" click="button2_clickHandler(event)"/>                
    </mx:VBox>
akhil_mittal
  • 23,309
  • 7
  • 96
  • 95