Question UploadFileAsync : WebException was unhandled

driekwartappel

New member
Joined
Dec 14, 2009
Messages
2
Programming Experience
1-3
Hi

On my windows application form I have these controls:
ProgressBar1 (progressbar)
lbl_message (label)
btn_submit (button)
btn_cancel (button)

when the btn_submit is clicked, the progressbar is populated and a file is FTP'd to its destination.

It is working fine, but sometimes when I click cancel to stop the FTP, I get the error :
"WebException was unhandled. The request was aborted: The request was canceled."

Below is my code and the ful error. Can someone please help!


VB.NET:
Imports System.ComponentModel
Imports System.Net

Public Class Form3
    Public client As New System.Net.WebClient()

    Private Sub Form3(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        ProgressBar1.Visible = False
        ProgressBar1.Value = 0
        lbl_message.Text = ""
        btn_submit.Enabled = True
    End Sub


    Private Sub btn_submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_submit.Click
        btn_submit.Enabled = False
        ProgressBar1.Visible = True
        btn_cancel.Text = "Cancel"
        lbl_message.Text = ""

        Try
            With client
                .Proxy = Nothing
                .Credentials = New NetworkCredential("username123", "password123")
                .UploadFileAsync(New Uri("ftp://test.com/temp.msg"), "C:\grabrelay\temp.msg")
                AddHandler client.UploadProgressChanged, AddressOf UpdateProgressBar
                AddHandler client.UploadFileCompleted, AddressOf OnFileuploadCompleted
            End With
        Catch ex As Exception
            MsgBox("Connection Failed: " & ex.Message)
            Me.Close()
        End Try
    End Sub


    Sub UpdateProgressBar(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs)
        If ProgressBar1.InvokeRequired Then
            ProgressBar1.Invoke(New UploadProgressChangedEventHandler(AddressOf UpdateProgressBar), sender, e)
            Exit Sub
        Else
            ProgressBar1.Value = CInt(ProgressBar1.Minimum + ((ProgressBar1.Maximum - ProgressBar1.Minimum) * e.ProgressPercentage) / 100)
        End If

        If lbl_message.InvokeRequired Then
            lbl_message.Invoke(New UploadProgressChangedEventHandler(AddressOf UpdateProgressBar), sender, e)
            Exit Sub
        Else
            lbl_message.Text = "File(s) busy being sent..."
        End If
    End Sub

    Private Sub OnFileuploadCompleted(ByVal sender As Object, ByVal e As UploadFileCompletedEventArgs)
        If e.Cancelled Then
            MsgBox("canceled e")
        ElseIf Not e.Error Is Nothing Then
            MsgBox("An error occur")
            Me.Close()
        Else
            RemoveHandler client.UploadProgressChanged, AddressOf UpdateProgressBar
            RemoveHandler client.UploadFileCompleted, AddressOf OnFileuploadCompleted
            If lbl_message.InvokeRequired Then
                lbl_message.Invoke(New UploadProgressChangedEventHandler(AddressOf UpdateProgressBar), sender, e)
                Exit Sub
            Else
                lbl_message.Text = "File sent successfully."
            End If
            Me.Close()
        End If
    End Sub

    Private Sub btn_cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_cancel.Click
        RemoveHandler client.UploadProgressChanged, AddressOf UpdateProgressBar
        RemoveHandler client.UploadFileCompleted, AddressOf OnFileuploadCompleted

        client.CancelAsync()

        MsgBox("Canceled")
        Me.Close()
    End Sub

End Class



'---------HERE IS THE COMPLETE ERROR---------------

System.Net.WebException was unhandled
Message="The request was aborted: The request was canceled."
Source="System"
StackTrace:
at System.Net.FtpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.FtpDataStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)
at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)
at System.Net.FtpDataStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at System.Net.WebClient.UploadBitsState.Close()
at System.Net.WebClient.UploadBitsWriteCallback(IAsyncResult result)
at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
at System.Net.ContextAwareResult.CompleteCallback(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.ContextAwareResult.Complete(IntPtr userToken)
at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
InnerException:
------------------------------
 

MattP

Well-known member
Joined
Feb 29, 2008
Messages
1,206
Location
WY, USA
Programming Experience
5-10
I'm pretty sure WebClient.CancelAsync calls Thread.Abort so it should be a matter of putting it in a Try/Catch block and catching the ThreadAbortException.

Edit: went and looked it up on msdn.

"If an operation is pending, this method calls Abort on the underlying WebRequest."

WebClient.CancelAsync Method (System.Net)
 
Last edited:

driekwartappel

New member
Joined
Dec 14, 2009
Messages
2
Programming Experience
1-3
Hi MattP

thanks for your response.

from msdn documentation you sent:
"If you cancel a string download operation and you check the Result property of the DownloadStringCompletedEventArgs object, an exception will occur."

I think thats the same thing i am doing, just with "UploadFileCompletedEventArgs".

So I tried catching the webexception & exception at Sub OnFileuploadCompleted, but I still get the same error.
Then i tried putting try/catch exception at "client.Cancel" (and in the rest of the Sub's, still same error)

Do you know if I should be adding something specific in the catch?
(i am just doing a "me.close" in the catch at the moment)
 
Top Bottom