2 Replies Latest reply on Jun 5, 2009 9:55 AM by Fredy Gadotti

    Show current progress

    Fredy Gadotti Level 1

      Hi,

       

      I made a sample to show what is my problem.

      Well, I'm trying to read recusivly directories to get the total files.

      But when the program is running, the progress bar isnt show and the applications stop until he finish the process.

      I'd like to show the current reading file and put a indeterminate progress bar while the progress isnt read.

       

      Goes snippet:

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" 
           layout="vertical"
           horizontalScrollPolicy="off"
           verticalScrollPolicy="off" 
           backgroundColor="white" height="136" width="350">
      
           <mx:Script>
                <![CDATA[
                     import mx.effects.Pause;
                     import mx.utils.ObjectUtil;
                     
                     [Bindable] private var isSearching:Boolean = false;
                     [Bindable] private var currentFile:String = "";
                     [Bindable] private var totalFiles:int = 0;
                     
                     private function searchDir(event:MouseEvent):void {
                          var file:File = new File();
                          
                          file.addEventListener(Event.SELECT, dirSelected);
                          file.browseForDirectory("Select a directory");
                          
                          function dirSelected(e:Event):void {
                              trace(file.nativePath);
                              isSearching = !isSearching;
                              getFilesInformation(file.nativePath);
                              currentFile = "";
                              isSearching = !isSearching;
                          }
                     }
                     
                     private function getFilesInformation(root:String):void {
                          var arq:File = new File(root);
                          var list:Array = arq.getDirectoryListing();
                          
                          for (var i:int = 0; i < list.length; i++){
                               var file:File = new File(list[i].nativePath);
                               
                               trace(file.nativePath);
                               if (file.isDirectory){
                                    getFilesInformation(file.nativePath);
                                    trace("is dir");
                               } else {
                                    totalFiles++;
                                    currentFile = file.nativePath;
                                    trace(file.name + " size: " + file.size);
                               }
                          }
                          
                     }
                     
                ]]>
           </mx:Script>
           <mx:Button click="searchDir(event)" label="Choose Dir" />
           <mx:Label text="{currentFile}" />
           <mx:Label text="{String(totalFiles)}" />
           <mx:ProgressBar indeterminate="true" visible="{isSearching}"/>
      </mx:WindowedApplication>
      
       
      

       

       

      Any advice?

       

      Regards, Fredy Gadotti.

        • 1. Re: Show current progress
          alevicki

          Your progress bar and application are locking up because AIR is single threaded, which means the UI has to wait for your function to finish before it can redraw the UI.  You will need to change to using the asynchronous methods that the File class offers.

           

          For instance, you should call arq.getDirectoryListingAsync() instead and then wait for the directoryListing event.

          1 person found this helpful
          • 2. Re: Show current progress
            Fredy Gadotti Level 1

            If someone have the same problem, here is the solution!

             

            Snippet

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" backgroundColor="white">
                 <mx:Script>
                      <![CDATA[
                      
                           [Bindable] private var isSearching:Boolean = false;
                           [Bindable] private var totalFiles:int = 0;
                           [Bindable] private var currentFile:String = "";
                           
                           private function searchDir(event:MouseEvent):void {
                                var file:File = new File();
                                
                                file.addEventListener(Event.SELECT, dirSelected);
                                file.browseForDirectory("Select a directory");
                                
                                function dirSelected(e:Event):void {
                                    trace(file.nativePath);
                                    isSearching = !isSearching;
                                    getFilesInformation(file.nativePath);
                                    isSearching = !isSearching;
                                }
                           }
                           
                           private function getFilesInformation(root:String):void {
                                var arq:File = new File(root);
                                arq.addEventListener(FileListEvent.DIRECTORY_LISTING, dirListHandler);
                                arq.getDirectoryListingAsync();
                                
                           }
                           
                           private function dirListHandler(event:FileListEvent):void {
                               var list:Array = event.files;
                               
                               for (var i:int = 0; i < list.length; i++){
                                     var file:File = new File(list[i].nativePath);
                                     
                                     trace(file.nativePath);
                                     if (file.isDirectory){
                                          getFilesInformation(file.nativePath);
                                          trace("is dir");
                                     } else {
                                          totalFiles++;
                                          currentFile = file.nativePath;
                                          trace(file.name + " size: " + file.size);
                                     }
                                }
                           }
                           
                      ]]>
                 </mx:Script>
                 <mx:Button click="searchDir(event)" label="Choose Dir" />
                 <mx:Label text="{currentFile}" width="200"/>
                 <mx:Label text="{String(totalFiles)}" />
                 <mx:ProgressBar indeterminate="true" visible="{isSearching}"/>
            </mx:WindowedApplication>
            
             
            

             

             

            Regards,

             

            Fredy Gadotti.