My upload code in BackgroundWoker:
Try 'Create FTP Request Dim myRequest As FtpWebRequest = DirectCast(WebRequest.Create("ftp://server" + "/" + "public_html/ftpfile" + "/" + fileName), FtpWebRequest) 'Update properties myRequest.Credentials = New NetworkCredential("username", "password") myRequest.Method = WebRequestMethods.Ftp.UploadFile 'Read the file Dim myFile As Byte() = File.ReadAllBytes(Pathh) 'Upload the file Dim myStream As Stream = myRequest.GetRequestStream() myStream.Write(myFile, 0, myFile.Length) 'Cleanup myStream.Close() myStream.Dispose() Catch srvrerror As Exception MsgBox("An error occurred whilst attempting to contact the server.", MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly, "App - Server Error") Me.Close()
My question is! When i'm trying to CancelAsync() in the middle of file upload i got my exception error, but when i leave uploading alone until it completes everything works fine. How can i make when somebody press 'Cancel' to stop uploading.
My Cancel button code:
If uploadworker.IsBusy Then If uploadworker.WorkerSupportsCancellation Then uploadworker.CancelAsync() End If End If
I know that i need to add this code somewhere but i don't know where, or?:
Do If uploadworker.CancellationPending Then e.Cancel = True Exit Do End If
I'm using this code above for my software update downloading, and works fine but i don't know where i need to add this for file uploading.
And yes, my BackgroundWorker supports cancellation.