Archived Forum Post

Index of archived forum posts

Question:

vb.net sftp upload with progress

Mar 20 '14 at 17:11

Hello

I am uploading a file using sftp. I have a progress bar and I am trying to get the progress to update.

I have enabled events and I set the callback event but it does not do anything

I have this at the top:

Dim WithEvents sftp As New Chilkat.SFtp

This is the OnPercentDone sub:

Private Sub sftp_OnPercentDone(sender As Object, args As Chilkat.PercentDoneEventArgs) Handles sftp.OnPercentDone
    pbCopyProgress.Value = args.PercentDone
    UpdateLogWindow("Percent Done = " + args.PercentDone.ToString)

End Sub

when the upload starts nothing happens to the progress bar. What am I doing wrong?


Answer

I found no problem. Here's the complete sample I used for testing:

Public Class Form1

Dim WithEvents m_sftp As Chilkat.SFtp

Private Sub m_sftp_OnPercentDone(sender As Object, args As Chilkat.PercentDoneEventArgs) Handles m_sftp.OnPercentDone
    ProgressBar1.Value = args.PercentDone
End Sub

Private Sub UploadWithProgressToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UploadWithProgressToolStripMenuItem.Click
    m_sftp = New Chilkat.SFtp()

    '  Any string automatically begins a fully-functional 30-day trial.
    Dim success As Boolean
    success = m_sftp.UnlockComponent("test")
    If (success <> True) Then

        TextBox1.Text = m_sftp.LastErrorText
        Exit Sub
    End If

    '  Set some timeouts, in milliseconds:
    m_sftp.ConnectTimeoutMs = 15000
    m_sftp.IdleTimeoutMs = 15000

    '  Connect to the SSH server.
    '  The standard SSH port = 22
    '  The hostname may be a hostname or IP address.
    Dim port As Long
    Dim hostname As String
    hostname = "192.168.1.127"
    port = 22
    success = m_sftp.Connect(hostname, port)
    If (success <> True) Then
        TextBox1.Text = m_sftp.LastErrorText
        Exit Sub
    End If

    '  Authenticate with the SSH server.  Chilkat SFTP supports
    '  both password-based authenication as well as public-key
    '  authentication.  This example uses password authenication.
    success = m_sftp.AuthenticatePw("chilkat", "xxx")
    If (success <> True) Then
        TextBox1.Text = m_sftp.LastErrorText
        Exit Sub
    End If

    '  After authenticating, the SFTP subsystem must be initialized:
    success = m_sftp.InitializeSftp()
    If (success <> True) Then
        TextBox1.Text = m_sftp.LastErrorText
        Exit Sub
    End If

    '  Upload from the local file to the SSH server.
    '  Important -- the remote filepath is the 1st argument,
    '  the local filepath is the 2nd argument;
    Dim remoteFilePath As String
    remoteFilePath = "hamlet.xml"
    Dim localFilePath As String
    localFilePath = "c:/aaworkarea/hamlet.xml"

    success = m_sftp.UploadFileByName(remoteFilePath, localFilePath)
    If (success <> True) Then
        TextBox1.Text = m_sftp.LastErrorText
        Exit Sub
    End If

    MsgBox("Success.")
End Sub

End Class


Answer

I am using a background worker, could that have something to do with it?


Answer

Most certainly. Research the topic of accessing/updating controls on the main thread from a background worker thread. For example: http://stackoverflow.com/questions/6937300/vb-net-winforms-how-to-access-objects-of-main-thread-from-backgroundworkers-t


Answer

I figured it out. I was declaring the sftp inside the sub and that was overwriting the sftp that had the Dim WithEvents