0 votes

I recently used the ftp.dll, ok for general functionality,
however I am unable to animate a progress bar during an upload.

Is it possible to have a VB.net example or help?

I use this vb.net code, it doesn't seem to work:

AddHandler client.Progress, AddressOf Ftp_Progress

Private Sub InviaFile(ByVal mFile As String)
    Bar.Value = 0
    Dim fileStream As FileStream = Nothing
    fileStream = File.Open( _ 
        CurDir() & "\Invio_data\" & mFile,  _ 
        FileMode.Open)

    Bar.Maximum = fileStream.Length
    fileStream.Close()

        If Not client.FileExists(mFile) Then
            client.Upload( _ 
                mFile, _ 
                CurDir() & "\Invio_data\" & mFile)
        End If

End Sub

Private Sub Ftp_Progress( _ 
    ByVal sender As Object, _ 
    ByVal e As ProgressEventArgs

    If e.Percentage <> -1 Then
       Bar.Value = CInt(e.Percentage)
    End If
End Sub

thank you

by (200 points)

1 Answer

0 votes

Here's the code. Most likely you forgot to use BeginInvoke:

Imports System.IO
Imports System.Threading
Imports Limilabs.FTP.Client

Public Class Form1
    Inherits Form

    Private ReadOnly _btnUpload As Button
    Private ReadOnly _progressBar As ProgressBar
    Private ReadOnly _listBox As ListBox

    Public Sub New()
        _btnUpload = New Button()
        _btnUpload.Text = "Upload"
        _progressBar = New ProgressBar()
        _listBox = New ListBox()
        _btnUpload.Dock = DockStyle.Bottom
        _listBox.Dock = DockStyle.Fill
        _progressBar.Dock = DockStyle.Top

        Me.Controls.Add(_listBox)
        Me.Controls.Add(_progressBar)
        Me.Controls.Add(_btnUpload)

        AddHandler _btnUpload.Click, AddressOf btnUpload_Click
    End Sub

    Private Sub Ftp_Progress(
        ByVal sender As Object, 
        ByVal e As ProgressEventArgs)

        Me.BeginInvoke(CType(Sub()
            _progressBar.Value = CInt(e.Percentage)
            End Sub, MethodInvoker))
    End Sub

    Private Sub btnUpload_Click(
        ByVal sender As Object, 
        ByVal e As EventArgs)

        Dim thread As Thread = New Thread(
            Sub()
                Try
                    Using ftp As Ftp = New Ftp()
                        AddHandler ftp.Progress, 
                            AddressOf Ftp_Progress
                        ftp.Connect("localhost")
                        Log("Connected. Logging in...")
                        ftp.Login("ftptest", "cypher")

                        For Each fileName As String In Directory.GetFiles(".")
                            ftp.Upload(
                                Path.GetFileName(fileName),
                                fileName)
                        Next

                        ftp.Close()
                    End Using

                Catch ex As Exception
                    Log(ex.ToString())
                End Try
            End Sub)
        thread.Start()
    End Sub

    Private Sub Log(ByVal entry As String)
        Me.BeginInvoke(
            CType((Function() _listBox.Items.Add(entry)),
            MethodInvoker))
    End Sub

End Class
by (300k points)

Thanks for your answer
I used :

BeginInvoke(CType(Sub() Bar.Value = CInt(e.Percentage), MethodInvoker))

but the coffin does not animate, I think the problem lies in the compatibility of the "Bar.Maxmum" and "e.Percentage" sets, I think that the one that e.Percentage returns is not compatible with the value assigned to Bar.Maxmum so you see nothing.
in my case Bar.Maxmum = fileStream.Length which the size of the stream in bytes, in the example fileStream.Length returns 17447875
so Bar.Maxmum = 17447875.
CInt(e.Percentage) which returns ?

ProgressEventArgs.Percentage gets the percentage of the finished job (0 - 100). If TotalBytesToTransfer is not set, returns -1.

...