4 Replies Latest reply on Sep 3, 2010 7:17 AM by miguel8312

    update progress bar while looping

    miguel8312 Level 3

      I finally got it through my thick skull flex is single threaded. GOT IT!!! however there has got to be a way to update the interface while in a loop. i have the following code that does not seem to work. I know there is got to be a  better way but i cant seem to pinpoint it out.

      Can anyone offer an example or any ideas. I have tried doing the following and it just does not work...

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application
      
       xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
      
      
      
      <mx:Script>
      
                <![CDATA[
      
      import flash.events.TimerEvent;
      
      
      import flash.utils.Timer;
      
      import flash.utils.setTimeout;
      
      private var delay:Number = 0; // delay before calling myDelayedFunction
      
      
      import mx.controls.Alert;
      
      private var fr:FileReference;
                     [
      
      Bindable]
      
      public var data1:ByteArray;
      
      
      
      public var timer:Timer = new Timer( 10000 );
      
      
      public var count:Number = 0;
      
      
      public function init():void
                     {
      
      this.timer.addEventListener( TimerEvent.TIMER, updateProgress );
      
      
      this.timer.start();
                     }
      
      
      
      protected function updateProgress(value:int):void
                     {
                          count++;
      
                          progress2.setProgress( (value % 10), 100 );
                          progress2.label = (value % 10) + 
      "%";                    progress2.invalidateDisplayList();
      
      
      
      
      
                     }
      
      
      
      
      //File types which we want the user to open
      
      private static const FILE_TYPES:Array = [new FileFilter("Text File", "*.csv;*.text")];
      
      
      //called when the user clicks the load file button
      
      private function onLoadFileClick():void
                     {
      
      // fr.addEventListener(ProgressEvent.PROGRESS, onProgress);
      
      
      //create the FileReference instance
                          fr = 
      new FileReference();
      
      
      //listen for when they select a file
                          fr.addEventListener(Event.SELECT, onFileSelect);
      
                          fr.addEventListener(ProgressEvent.PROGRESS, onProgress);
      
      //listen for when then cancel out of the browse dialog
                          fr.addEventListener(Event.CANCEL,onCancel);
      
      
      //open a native browse dialog that filters for text files
                          fr.browse(FILE_TYPES);
                     }
      
      
      /************ Browse Event Handlers **************/
      
      
      
      //called when the user selects a file from the browse dialog
      
      private function onFileSelect(e:Event):void
                     {
      
      
      
      //listen for when the file has loaded
                          fr.addEventListener(Event.COMPLETE, onLoadComplete);
      
      // listen forprogress
                          fr.addEventListener(ProgressEvent.PROGRESS, onProgress);
      
      
      //listen for any errors reading the file
                          fr.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
      
      
      //load the content of the file
                          fr.load();
      
      // listen to write to arrays
                          fr.addEventListener(ProgressEvent.PROGRESS, onProgress);
                          fr.addEventListener(Event.COMPLETE, eventComplete);
                          fr.addEventListener(ProgressEvent.PROGRESS, onProgress);
      
                     }
      
      
      
      private function onLoadComplete(e:Event):void
                     {
      
      //get the data from the file as a ByteArray
      
      //myData = new ArrayCollection();
      
                          data1 = fr.data;
      
      
      //outputField.text = data1.readUTFBytes(data1.bytesAvailable);
      
                     }
      
      
      //called if an error occurs while loading the file contents
      
      private function onLoadError(e:IOErrorEvent):void
                     {
      
      trace("Error loading file : " + e.text);
                     }
      
      
      
      private function onProgress(event:ProgressEvent):void{
      
      //          pbProgress.visible = true;
      
      var bloaded:uint = new uint;
      
      
      var btotal:uint = new uint;
                          bloaded = event.bytesLoaded;
                          btotal = event.bytesTotal;
      
      
                     }
      
      
      
      private function onCancel(e:Event):void
                     {
      
      trace("File Browse Canceled");
                          fr = 
      
      null;               }
      
      
      
      
      public function eventComplete(event:Event):void{
      
      
      var record:Array = new Array();
      
      
      var obj:Object;
      
      var fields:Array = new Array();
      
      
      var result:String = new String(fr.data);                    record = result.split(
      
      "\r\n");
      
      
      for ( var i:int = 0; i<record.length; i++)// ++ stands for increment
                               {
                               obj = 
      new Object();                         fields = record[i].split(
      
      ",");// separate each field by comma.
                               obj.col1  =  fields[0];
      
      trace(obj.col1);
                               count=i;
                               updateProgress(count)
      
      
      //setTimeout(updateProgress,delay);
      
      //callLater(updateProgress);
      
                          }
      
                          }
      
                ]]>
      
      </mx:Script>
      
      
      
      <mx:ProgressBar id="progress2" minimum="100" maximum="100" mode="manual"                         label="
      
      0%" labelPlacement="bottom">
      
      
      </mx:ProgressBar>
      
      
      <mx:Button label="clickme" click="onLoadFileClick()"/>
      
      
      
      </mx:Application>
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      

       

        • 1. Re: update progress bar while looping
          Flex harUI Adobe Employee

          Try updateAfterEvent

          • 2. Re: update progress bar while looping
            miguel8312 Level 3

            do you have any sample code to show me how to use this.???

             

            I was able to acomplish this by litterally breaking everytime it loops. look below i dont know if this is the best way to do it. but it works for me. 

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
                  
                  <mx:Script>
                        <![CDATA[
                              import flash.events.TimerEvent;
                              import flash.utils.Timer;
                              import flash.utils.setTimeout;
                              private var delay:Number = 2000; // delay before calling myDelayedFunction
             
                              import mx.controls.Alert;
                              private var fr:FileReference;
                              [Bindable]
                              public var data1:ByteArray;
                              
                              public var timer:Timer = new Timer(10);
                              public var count:Number = 0;
                              
                              public function init():void
                              {
                                    this.timer.addEventListener( TimerEvent.TIMER, updateProgress );
                                    
                                    this.timer.start();
                                    
                              }
                              
                              protected function updateProgress(time:Event):void
                              {
                                    //count++;
                                    
                                    //progress2.setProgress( ((count/record.length% 10)),(record.length));
                                    progress2.setProgress(count,record.length)
                                    //progress2.setProgress( (count% 10), 100 );
                                    progress2.label = Math.round(count/record.length * 100).toString()+"%";
                                    progress2.invalidateDisplayList();
                                    timer.stop()
                                    resumeLoop();
                                    
                                    
                                    
                              
                              }
                        
                        
                              //File types which we want the user to open
                              private static const FILE_TYPES:Array = [new FileFilter("Text File", "*.csv;*.text")];
                              
                              //called when the user clicks the load file button
                              private function onLoadFileClick():void
                              {
                                    // fr.addEventListener(ProgressEvent.PROGRESS, onProgress);
                                    
                                    //create the FileReference instance
                                    fr = new FileReference();
                                    //listen for when they select a file
                                    fr.addEventListener(Event.SELECT, onFileSelect);
                                    
                                    fr.addEventListener(ProgressEvent.PROGRESS, onProgress);
                                    //listen for when then cancel out of the browse dialog
                                    fr.addEventListener(Event.CANCEL,onCancel);
                                    
                                    //open a native browse dialog that filters for text files
                                    fr.browse(FILE_TYPES);
                              }
                              
                              /************ Browse Event Handlers **************/
                              
                              //called when the user selects a file from the browse dialog
                              private function onFileSelect(e:Event):void
                              {
                                    
                                    
                                    //listen for when the file has loaded
                                    fr.addEventListener(Event.COMPLETE, onLoadComplete);
                                    // listen forprogress
                                    fr.addEventListener(ProgressEvent.PROGRESS, onProgress);
                                    
                                    //listen for any errors reading the file
                                    fr.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
                                    
                                    //load the content of the file
                                    fr.load();
                                    // listen to write to arrays
                                    fr.addEventListener(ProgressEvent.PROGRESS, onProgress);
                                    fr.addEventListener(Event.COMPLETE, eventComplete);
                                    fr.addEventListener(ProgressEvent.PROGRESS, onProgress);
                                    
                              }
                        
                        
                              private function onLoadComplete(e:Event):void
                              {
                                    //get the data from the file as a ByteArray
                                    //myData = new ArrayCollection();
                                    
                                    data1 = fr.data;
                                    
                                    //outputField.text = data1.readUTFBytes(data1.bytesAvailable);
                                    
                              }
                              
                              //called if an error occurs while loading the file contents
                              private function onLoadError(e:IOErrorEvent):void
                              {
                                    trace("Error loading file : " + e.text);
                              }
                        
                              private function onProgress(event:ProgressEvent):void{
                                    //          pbProgress.visible = true;
                                    var bloaded:uint = new uint;
                                    var btotal:uint = new uint;
                                    
                                    bloaded = event.bytesLoaded;
                                    btotal = event.bytesTotal;
                                                      
                                    
                              }
                        
                              private function onCancel(e:Event):void
                              {
                                    trace("File Browse Canceled");
                                    fr = null;
                              }
                              
                              public var recordPointer:int;
                              public var totalCount:int;
                              public var record:Array = new Array();
                              public var obj:Object;
                              public var fields:Array = new Array();
                              public var i:int;
                              
                              public function eventComplete(event:Event):void{
                                    resumeLoop();
                                    
                                    
                                    }
                              
                                    
                                    
                              
                              public function resumeLoop():void{
                                    
                                    
                                    var result:String = new String(fr.data);
                                    record = result.split("\r\n");
                                    if (record.length == i) {done();}
                                    else{
                                    for ( i<record.length; i++;)// ++ stands for increment
                                    {
                                          if (i == record.length){break};
                                          //setTimeout(updateProgress,delay);
                                          obj = new Object();
                                          fields = record[i].split(",");// separate each field by comma.
                                          obj.col1  =  fields[0];
                                          trace(obj.col1);
                                          count=i;
                                          
                                          break;
                                    }
                                    }
                                    if (i == record.length){done()}
                                    else{
                                    
                                          this.timer.addEventListener( TimerEvent.TIMER, updateProgress );
                                          
                                          this.timer.start();
                                          
                                          } 
                              }
                              
                              public function done():void
                              {
                               
                              
                              
                              }
                              
                                    
                        ]]>
                  </mx:Script>
                  
                  <mx:ProgressBar id="progress2" minimum="100" maximum="100" mode="manual"
                                          label="0%" labelPlacement="bottom">
                  </mx:ProgressBar>
                  <mx:Button label="clickme" click="onLoadFileClick()"/>
                        
                  
            </mx:Application>
            
            
            • 3. Re: update progress bar while looping
              Flex harUI Adobe Employee

              I'm not sure why you needed to stop and start.  Calling

              time.updateAfterEvent should have helped.

              • 4. Re: update progress bar while looping
                miguel8312 Level 3

                Flex harUI  im sure your right but i did not have any luck using updateafterevent. can

                you please put together a little example?  i really love to see how you would do

                it it using that method... hope to hear back from you im eager to learn and love to see how you

                would acomplish with one function as supposed to 3 functions like i did.

                Miguel