1 Replies - 505 Views - Last Post: 19 December 2013 - 05:49 PM Rate Topic: -----

#1 Neph1x  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 110
  • Joined: 19-November 11

FTP upload in BackgroundWorker

Posted 17 December 2013 - 05:51 PM

Hey, i need a little help with FTP uploading with backgroundWorker, more precisely if someone wants to stop uploading!

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.


Thank you..

Is This A Good Question/Topic? 0
  • +

Replies To: FTP upload in BackgroundWorker

#2 poncianux  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-January 11

Re: FTP upload in BackgroundWorker

Posted 19 December 2013 - 05:49 PM

You can try disposing the backgroundworker, but I think it wouldn't works.
It's because upload it's a single call I don't think you can get access and put a condition inside a while or a for.

Try to make the variable myStream global and check if you can stop it in another function!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1