Copying Folders Freezes Application

Hi,

I have a submit button with the following action code:

  dim src as FolderItem
  dim dest as FolderItem
  src=SpecialFolder.Desktop.Child("FolderA")
  dest=SpecialFolder.Desktop.Child("FolderB")
  
  COPYFOLDER(src,dest)

and a method (passed on clicking above button.

Method Name: COPYFOLDER
Params: src as FolderItem, dest as FolderItem

  dim m as integer
  for m = 1 to src.count
    if src.item(m) <> nil then
      if src.item(m).directory then
        dest.child(src.item(m).name).createAsFolder
        CopyFolder(src.item(m), dest.child(src.item(m).name))
      else
        src.item(m).copyFileTo dest
      end if
    end if
  next

All works ok, but the application seizes up (assuming because this is not asyncronus?). Can someone point me in the right direction that doesn’t include a £200.00 plugging from Monkeybread please….

You could try putting this loop in a Thread. You’ll need to bone up on how to properly integrate that with your app’s UI though. Typically, you’d use a timer and progress bar to show progress. You might do that modally. You don’t have to.

Dear I say:

add ‘App.doevents(10)’ in the for loop. :slight_smile:

I know it is not ‘done’ but it does help. I do use this sometimes and I have yet to see any strange things happening.

At Brad said, put your code in a thread. And if you want to save a lot of your time, then spend some looking at the Task Thread subclass described in the Wiki and demoed in the Example Projects/Desktop/UpdatingUIFromThread/UIThreadingWithTask example.

…and if you don’t fancy a thread, you might shell out to an operating system call, or create a tiny little app which does nothing but read the command line for two folder names, and then does the copy. (helper app)
Launch the app, then forget about it.
(Your code doesn’t seem to try to update the UI with progress anyway)

Helper app is obviously less useful if you need to know when the copy is complete, tho… :slight_smile:

[quote=48412:@Jeff Tullin]…and if you don’t fancy a thread, you might shell out to an operating system call, or create a tiny little app which does nothing but read the command line for two folder names, and then does the copy. (helper app)
Launch the app, then forget about it.
(Your code doesn’t seem to try to update the UI with progress anyway)

Helper app is obviously less useful if you need to know when the copy is complete, tho… :)[/quote]

You can even use a bash script and copy with a bash command :wink:

[quote=48365:@Christoph De Vocht]add ‘App.doevents(10)’ in the for loop. :slight_smile:
[/quote]

Don’t do that.

Yes I know but it does help sometimes when a thread is a bit more difficult to implement.
ATM I have never got any issues using it. :slight_smile:

“Using DoEvents in a GUI application will likely cause instability.” (emphasis in original.)

http://documentation.xojo.com/index.php?title=Application.DoEvents&oldid=44706