views:

34

answers:

2

Hi All,

I have a recursive call which includes an asyn operation (file copy) ..
I want to find out when the recursive call finishes (along with all asyn operations).

    private function copyInto(directoryToCopy:File, locationCopyingTo:File):void
        {
          var directory:Array = directoryToCopy.getDirectoryListing();
          //get list of files in dir 

          for each (var f:File in directory)
          {
            if (f.isDirectory)
              copyInto(f, locationCopyingTo.resolvePath(f.name));
            else
             {
               f.copyToAsyn(locationCopyingTo.resolvePath(f.name), true);
               f.addEventListener(Event.COMPLETE, onFilecopied);
              }
          }    
        }

function onFileCopied(event){ 
    alert(event.target); // [object File] 
} 

Basically what I needed was to copy a folder structure to another drive,and the folder being copied contains some files that are 100-200MB and which take time to copy,which blanks out the UI and the flash player.
And after the files get copied, I want to do further things(hopefully without blocking the UI).

Any help?

Thanks All .

+1  A: 

You could set a static class-level counter of files to be copied that increments when you find another file to copy, and decrements when the event function signals that the copy has ended.

private static fileCounter:int = 0;

and in the loop:

++fileCounter;
f.addEventListener(Event.COMPLETE, onFilecopied);
f.copyToAsync(locationCopyingTo.resolvePath(f.name), true);

and in the event handler:

function onFileCopied(event)
{ 
    if ( --fileCounter == 0 )
        trace("finished");
} 

But is weak because you can't recover from errors, nor know which files have been copied. To build a more robust solution you could store not only a counter of files but a list of file names in a class:

class FileCopyInfo
{
...
    private var fileName:String;
    private var finished:Boolean;
}
vulkanino
+1 thx for answer :)
Amitd
+1  A: 

Count the calls of copyToAsync and use a wrapper function like doCopy in my example.

private var copyCount: int = 0;
private var isCopying: Boolean = false;

public function doCopy(directoryToCopy:File, locationCopyingTo:File):void
{
    isCopying = true;
    try {
      copyInto((directoryToCopy, locationCopyingTo);
    finally {
      isCopying = false;
    } 
}

private function copyInto(directoryToCopy:File, locationCopyingTo:File):void {
    var directory:Array = directoryToCopy.getDirectoryListing();

    for each (var f:File in directory) {
        if (f.isDirectory)
            copyInto(f, locationCopyingTo.resolvePath(f.name));
        else {
            copyCount++; 
            f.addEventListener(Event.COMPLETE, onFileCopied);
            f.copyToAsync(locationCopyingTo.resolvePath(f.name), true);
        }
    }    
}

function onFileCopied(event: Event): void
{ 
    copyCount--;
    if (copyCount == 0 && !isCopying)
        trace("finished");
} 

Important: The isCopying flag checks if the copyInto function is still running, otherwise you could run into to a condition where copyCount == 0, but the copying is not finished.

splash
+1 thx for answer :) accepted
Amitd