Why is DownloadCallback called twice?

0 votes
asked Jun 19, 2012 by SuperMike (280 points)
edited Jun 20, 2012

I am making some tests with SFTP. My BeginDownload() definition is like this:

            oSftp.BeginDownload(lcFile, "d:\", _
             Rebex.IO.TraversalMode.Recursive, Rebex.IO.TransferMethod.Copy, Rebex.IO.ActionOnExistingFiles.ThrowException, _
             New AsyncCallback(AddressOf DownloadCallback), Nothing)

As you can see, there is a callback to the DownloadCallback() method.

This method is defined like this:

' Callback for download finished
Public Sub DownloadCallback(ByVal asyncResult As IAsyncResult)

    MessageBox.Show("DownloadCallback")

    Try
        oSftp.EndDownload(asyncResult)
        SafeInvoke(New TransferFinishedDelegate(AddressOf TransferFinished), New Object() {Nothing, False})
    Catch ex As Exception
        SafeInvoke(New TransferFinishedDelegate(AddressOf TransferFinished), New Object() {ex, False})
    End Try

    oSftp.Dispose()
    oSftp = Nothing
    _isWorking = False
End Sub

My test is about testing the download of a file when it already exists. The test works well on the first click on my Test button on the form. I can get the message as shown in the DownloadCallback() method.

Then, in my TransferFinished() method, I can get the reason of the failure saying that the file already exists. The TransferFinished() method is like this:

Private Sub TransferFinished(ByVal err As Exception, ByVal refreshSftp As Boolean)

    ' If we got a success
    If err Is Nothing Then
        MessageBox.Show("Transfer completed")
    Else
        Dim ex As Rebex.Net.SftpException = CType(err, Rebex.Net.SftpException)

        If (Not ex Is Nothing) AndAlso ex.Status = Rebex.Net.SftpExceptionStatus.OperationAborted Then
            _totalBytesTransferred += ex.Transferred
        End If

        MessageBox.Show("The transfer was not completed. " + oApp.cCR + err.Message)
    End If

End Sub

So, basically, if the local file already exists, I get this:

"DownloadCallback" "The transfer was not completed. File with the same name already exists..."

So far so good, now, if I try to click on the button again, I get two calls to DownloadCallback() which results in two calls to TransferFinised(). So, this gives two messages back saying "DownloadCallback". Then, once I click on each of them, I get two messages saying "The transfer was not completed. File with the same name already exists...". And, then, the application crashes.

Why is it sending two calls to DownloadCallback() on the second click? I need to be able to execute several operations without having to run my application everytime. So far, to make this work, I have to run the application, close it, run it, close it, run it, etc.

Applies to: Rebex SFTP

1 Answer

0 votes
answered Jun 19, 2012 by Lukas Matyska (51,690 points)
edited Jun 20, 2012

I have tried it myself and the callback method was executed ones for each click. It seems that you are calling the BeginDownload method more than once when you click on your button for the second time.

Please add the MessageBox.Show("Starting download...") call just before the oSftp.BeginDownload() method to see how many times the BeginDownload method was called.

commented Jun 19, 2012 by SuperMike (280 points)
edited Jun 20, 2012

Problem was related to something else. This is fixed.

...