0

I've created an app with a process. If the user tries to close the form when the process is busy they are prompted and asked if they'd like to wait or end. If they choose to wait then raise a cancel flag is set and checked in the BGW which will come to end after the current task.

The problem is how to deal with if they still like to end. My process is zipping up files. If the process is on a large zip and the user wouldn't like to wait then I need to kill the BGW and then clean up i.e. delete the incomplete zip file. I've written this:

Else
    ' If cancel selected...
    ' Forces backup to end.
     ???
     ' Deletes incomplete backup.
     If My.Computer.FileSystem.FileExists(currentBackup1) Then
         My.Computer.FileSystem.DeleteFile(currentBackup1)
     End If
End If

I've tried deleting the file in my form close sub, but the BGW still has access and I have an exception that the file is in use. So either I need to kill the BGW or be able to delete a file in use just before the form closes. Can anyone help please? Thanks.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
user3688529
  • 97
  • 1
  • 8
  • where is the code to cancel, or is that what you are looking for? – Ňɏssa Pøngjǣrdenlarp Jul 13 '14 at 20:45
  • possible duplicate of [How to "kill" background worker completely?](http://stackoverflow.com/questions/800767/how-to-kill-background-worker-completely) – Bjørn-Roger Kringsjå Jul 13 '14 at 20:45
  • That's what I'm looking for. I'm already using the BGW.CancelAsync() to raise the cancel flag in the BGW and to exit after the current loop. But to forcibly exit, perhaps not to wait for a long zip if you want to cloase the program. Now I know me.close() will accomplish that... But if a zip is underway I would like a chance to delete the incomplete zip file that is of no use. Any thoughts? – user3688529 Jul 13 '14 at 20:50
  • Delete the file in the catch block. – Bjørn-Roger Kringsjå Jul 13 '14 at 20:55
  • the BGW does not have the file open - **you** do. I dont know how you are looping on the files, but you should look at `CancellationPending` and respond by stopping the loop, closing the Zip and deleting it as part of the cancel process. Then exit the DoWork sub cleanly. – Ňɏssa Pøngjǣrdenlarp Jul 13 '14 at 21:03
  • 1
    Thanks for your replies. I've not used catch, until now anyway. But that just allows the program to close without the exception when it can't delete, but I need to actually delete the file. – user3688529 Jul 13 '14 at 21:03
  • @Plutonix I've used cancellation pending to exit the loop after the current zip is completed and to ignore the remaining folders to zip in a loop. But, i need to stop the program when a zip is in operation if the user doesn't want to wait for the zip to complete and then end. Hope that makes better sense. – user3688529 Jul 13 '14 at 21:06
  • 1
    since ZipFile's granularity is apparently Directory, you are kind of stuck. That might be something to look for in a replacement Lib - a way to specify certain files so you could feed them a few at a time and look for the cancel flag in between. You are right though, until the app ends you/NET/ZipFile class has that open and you wont be able to delete it. Maybe you could work from a temp folder and Move the finished product to the desired location; on start up you delete any old remnants there. – Ňɏssa Pøngjǣrdenlarp Jul 13 '14 at 21:23
  • Yeah, I will do something similar. Perhaps set a flag to say the program ended with completing the task, inform the user on next run, and ask if they'd like to delete the the remnant then. Thanks for your input as always! – user3688529 Jul 13 '14 at 21:36

1 Answers1

0

You can declare a variable that returns true if the process completed. So you can try like this

Public Class MainForm
Public IsComplete As Boolean = False

Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    ' Your code comes here
    IsComplete = True
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    If IsComplete Then
        MsgBox("Completed successfully")
    Else
        If My.Computer.FileSystem.FileExists(currentBackup1) Then
            My.Computer.FileSystem.DeleteFile(currentBackup1)
        End If
    End If
End Sub 
End Class
Eldar Zeynalov
  • 379
  • 5
  • 19