Progressbar not updating :'(
please help.

Private Sub ftpupload()
        Dim ext As String = Path.GetExtension(NsTextBox1.Text)
        Dim fname As String = Path.GetFileNameWithoutExtension(NsTextBox1.Text)
        Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://xxx@xxx.org/" & fname & ext), System.Net.FtpWebRequest)
        request.Credentials = New System.Net.NetworkCredential("xxx@xxx.org", "xxxxxxx")
        request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
        Dim file() As Byte = System.IO.File.ReadAllBytes(NsTextBox1.Text)
        Dim strr As System.IO.Stream = request.GetRequestStream()
        Do While i <= 100
            strr.Write(file, 0, file.Length)
            progress = strr.Length * i / 100
            BackgroundWorker1.ReportProgress(progress)
        Loop
    End Sub


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        BackgroundWorker1.WorkerReportsProgress = True
        loadauthors()
    End Sub


  Private Sub xButton1_Click(sender As Object, e As EventArgs) Handles NsButton1.Click

        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        xProgressBar1.Value = e.ProgressPercentage
    End Sub


    Private Sub BackgroundWorker1_DoWork(sender As Object, e As ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        ftpupload()
    End Sub

Do receive any update, or just completion without any status change on your progress bar?

Edited 2 Years Ago by Begginnerdev

Check the value "progress". Can also try adding "refresh" after "xProgressBar1.Value = e.ProgressPercentage". If you are using a StatusStrip, call StatusStrip1.Refresh().

-Hi Bud,

It looks like you are writing all the file bytes at once

strr.Write(file, 0, file.Length)

Typically when you want to generate a progress you need to write chunks of the file eg...

 Using fs As New FileStream(localFile, FileMode.Open, FileAccess.Read, FileShare.None)

                'This is the buffer that will hold the chunks to write
                Dim buffer() As Byte = New Byte(2047) {}

                'Only read 2kb of data per pass (thisfilesize is the length of the local file)
                For offset As Integer = 0 To thisfilesize Step 2048
                    'Let's read 2kb of the local file
                    fs.Read(buffer, 0, buffer.Length)

                    'If the last piece of data is less then 2kb then we'll reduce the chunksize
                    Dim chunkSize As Integer = thisfilesize - offset
                    If chunkSize > 2048 Then chunkSize = 2048

                    'Write the buffer to the remote stream (Buffer Size in this case 2kb)
                    clsStream.Write(buffer, 0, chunkSize)

                    'Update progress if int(percent complete) has changed
                    If prevPerc <> Int((100 / thisfilesize) * (offset + chunkSize)) Then
                        Console.WriteLine(Int((100 / thisfilesize) * (offset + chunkSize)))


                        If boo_RunOnThread = True Then
                            setProgressValue(Int((100 / thisfilesize) * (offset + chunkSize)))
                        Else
                            prg_ProgresBar.Value = Int((100 / thisfilesize) * (offset + chunkSize))

                        End If

                        prevPerc = Int((100 / thisfilesize) * (offset + chunkSize))

                    End If

                Next

            End Using
                Next

I hope this helps. Let me know if you have figured it out already or you need any help at all.

This article has been dead for over six months. Start a new discussion instead.