Chris128

Hi All,

I have a form that starts a background worker when it loads and basically acts as a progress form to show how far the file copy has got (it copies various files to various locations so I dont want to use the standard windows copy dialog box cos I'll have loads appearing). If the user clicks cancel or closes the form I want the operation to cancel, which I have done by testing whether or not the worker is cancellationpending before each file copy starts. So if it is cancel pending then I just exit the sub and close the form. In some cases though if there are a lot of files to copy then this means files will still be being copied in the background without the user knowing because the form has closed. So basically I want a way of being able to keep the form open (displaying "cancelling..." or something similar) temporarily until the copy has finished. Or a way to cancel the file copy immediatley would be even better. I have tried using the worker.isbusy property but this doesnt work, it just says the worker isnt busy as soon as the form closes even if there are still files being copied.

Thanks
Chris



Re: Visual Basic Language How to tell when directorycopy has finished

Jeff B.

Chris,

Have you set the WorkerSupportsCancellation property of your background worker class to True






Re: Visual Basic Language How to tell when directorycopy has finished

rkimble

You didn't tell us HOW you are copying the files! If you use System.IO.File.Copy() in a loop you should be able to test for the cancelation and exit the loop before the next call to Copy() - in this way the form will not respond to the cancel command issued by the user until after the current copy completes.




Re: Visual Basic Language How to tell when directorycopy has finished

Chris128

Currently Im using my.computer.filesystem.copydirectory to copy all the files in the directory. So are you saying instead of using that I should use a loop like:

For each file in directory

if backgroundworker.cancellationpending = false then

System.IO.File.Copy(file)

Else

exit sub

Next

Sorry if some of the methods or properties arent named exactly right, thats just off the top of my head and im not exactly an expert..




Re: Visual Basic Language How to tell when directorycopy has finished

Chris128

yep




Re: Visual Basic Language How to tell when directorycopy has finished

rkimble

Yes, that is what I would suggest. As you see, this gives the worker the chance to cancel between each file copy operation. The code might look something like:

Code Snippet

Dim src As String = "Path to source directory"

Dim dst As String = "Path to destination directory"

'Instead of using:

'My.Computer.FileSystem.CopyDirectory(src, dst)

'Try using:

Dim files() As String

files = System.IO.Directory.GetFiles(src)

For Each file As String In files

If Me.BackgroundWorker1.CancellationPending Then

Exit For

End If

System.IO.File.Copy(file, System.IO.Path.Combine(dst, System.IO.Path.GetFileName(file)))

Next

Hope that helps.