2 Replies Latest reply on Sep 17, 2009 11:38 AM by flexchief

    Complete event not fired when finished copying file to location

    flexchief Level 1

      Hi,

       

      I'm copying a file from one place to another on my local disk and want to be alerted when it finished being transfered but for some reason my complete event never fires?

       

      Here is my code:

       

          private function writeToFile():void
          {

                  trackFile = new File(C:\\File1.txt);
                  var newFile:File = new File(C:\\TestDir\\File1.txt);
                  trackFile.addEventListener(Event.COMPLETE, onCompleteHandler);
                  trackFile.copyTo(newFile, true);

          }

       

           private function onCompleteHandler(event:Event):void

           {

                Alert.show("Transfer complete!");

           }

       

       

      Any help would be great as I am really baffled as to where I'm going wrong with this one?

       

      Thanks

      Adam

      Flexchief

        • 1. Re: Complete event not fired when finished copying file to location
          adobe_paul Adobe Employee

          The reason you're not getting any event is because you're using the synchronous copyTo() version of the method instead of the asynchronous copyToAsync() version. The synchronous version is "blocking" which means that AIR pauses on that line of code until the operation finishes, and then moves on to the next line of code. So as soon as the copyTo() method moves on to the next line of code, it is done copying the file.

           

          If you change your code to use copyToAsync(), on the other hand, when the code moves to the next line of code the copy operation won't necessarily be done already, and you have to wait for the event to know that it has finished.

           

          (Most APIs in AIR are either synchronous or asynchronous only, so this usually isn't an issue. And admittedly it's not always consistent -- for instance, with the SQLStatement class, the result event is dispatched even when you're working in synchronous execution mode.)

          • 2. Re: Complete event not fired when finished copying file to location
            flexchief Level 1

            Thanks a million Paul that is fantastic! Works as I thought it should now and I still have a small bit of my hair left too .

             

            I have worked a lot with sqlite db's this past while and, as you motioned, have never had to enforce the use of any async methods for complete events to be fired, so I never believed that could be my problem.

             

            Thank you so much again for your help!

             

            Adam