Don't close the form until the task has finished. In the Click event handler of the Cancel button just disable the button so it can't be pressed again and then cancel the BackgroundWorker. You then close the form in the BackgroundWorker's RunWorkerCompleted event handler, so it happens when the operation completes, cancelled or not.