Results 1 to 6 of 6

Thread: I need a Progress Bar for my Download/Upload manager

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2015
    Posts
    7

    Question I need a Progress Bar for my Download/Upload manager

    I can't figure out how to add a progress bar to my Download/Upload manager and I need some help.
    I've looked on YouTube and forums and can't figure it out.
    I have a button that will allow me to choose the location and then the download process gets sent to a background worker.
    Here is my code for the button:
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim args() As String = {"", ""}
                    Me.SaveFileDialog1.FileName = Me.ListBox2.SelectedItem
                    If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                        args(0) = Me.ListBox1.SelectedItem
                        args(1) = SaveFileDialog1.FileName
                        BackgroundWorker1.RunWorkerAsync(args)
                    End If
    End Sub
    Here is my code for the Background worker (Download Function):
    Code:
    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            Dim RemoteFileName As String = e.Argument(0).ToString
            Dim LocalFileName As String = e.Argument(1).ToString
            Dim request1 As FtpWebRequest = Nothing
            Dim response1 As FtpWebResponse = Nothing
            request1 = CType(WebRequest.Create("FTPURL" + ListBox2.SelectedItem), FtpWebRequest)
            request1.Credentials = New NetworkCredential("FTPUsername", "FTPPassword")
            request1.Method = Net.WebRequestMethods.Ftp.DownloadFile
            response1 = CType(request1.GetResponse(), FtpWebResponse)
            Using ResponseStream1 As Stream = response1.GetResponseStream()
                Using FileStream As FileStream = File.Create(LocalFileName)
                    Dim buff(1024) As Byte
                    Dim bytesRead As Integer = 0
                    While (True)
                        bytesRead = ResponseStream1.Read(buff, 0, buff.Length)
                        If (bytesRead = 0) Then Exit While
                        FileStream.Write(buff, 0, bytesRead)
                    End While
                End Using
            End Using
            e.Result = "Success!" 'This will be passed back to the RunWorkerCompleted event in the e.Result there...
        End Sub
    I need help putting in a progress bar for this download process without modifying my current code.
    I don't mind adding code as long as I don't have to modify my current code.

  2. #2
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    Re: I need a Progress Bar for my Download/Upload manager

    Make sure the background worker is set to ReportProgress. Inside the DoWork handler, periodically call ReportProgress to raise a ProgressChanged event back in the calling thread, and pass back a percentage value that you can use to update the progressbar.

    Here is a CodeProject page that deals with this:
    http://www.codeproject.com/Tips/8331...ogressBar-demo

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2015
    Posts
    7

    Re: I need a Progress Bar for my Download/Upload manager

    Alright so I'm really new to programming. Is there some code you can give me in Visual Basic not C# that I can use to get this working?

  4. #4
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    Re: I need a Progress Bar for my Download/Upload manager

    Okay, try your Google-fu, here... I searched on "BackgroundWorker ProgressBar VB Example" and found a couple of result pages that tell you what you want to know. For example, this one.

    Let's look at what you have to do.

    "Make sure the background worker is set to report progress." - This is a simple property you need to set, and intellisense will help you find it. In your case,

    Code:
    backgroundWorker1.WorkerReportsProgress = true

    This allows DoWork() to report back on its progress to the calling thread.

    Now, inside DoWork. What you need is to report the percentage completed, and in order to do that you need the size of the original file that is apparently being downloaded. So get the file size and store it in a variable called something like fileSizeOriginal. In this example, I'm going to do it dead simple, i.e. it reports progress after each read.

    To do this, you'll need to keep track of the total bytes read. So, just before the While loop in the DoWork thread, set TotalBytesRead = 0. After each read, increment the total, TotalBytesRead = TotalBytesRead + bytesRead. Then after the buffer is written to the output file, add lines that do this:

    Code:
    Dim Percentage as integer = 'Calculate the percentage complete as an integer 0 - 100, formula ==> (TotalBytesRead / fileSizeOriginal) * 100
    backgroundWorker1.ReportProgress(Percentage)
    Okay, so far we have set the background thread so it can report progress, and we report our progress as a percentage after every read. Now we have to go back to the calling thread and handle the report from the backgroundworker. We do this in the ProgressChanged event handler.

    Code:
    Private Sub backgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles backgroundWorker1.ProgressChanged
        prgPercentComplete.Value = e.ProgressPercentage
    End Sub
    This assumes you have a progress bar on the form called prgPercentComplete, and that prgPercentComplete.Minimum = 0 and prgPercentComplete.Maximum = 100.

    That's the basic idea. You could also report in, say, 10% intervals, by placing an in if statement in DoWork before the call to ReportProgress to determine if progress should be reported.

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2015
    Posts
    7

    Re: I need a Progress Bar for my Download/Upload manager

    Now it's only reporting the progress once.
    Under "Public Class Form1" I have
    Code:
    Dim Percentage As Integer
    Here's what I have on the While statement.
    Code:
    For i As Integer = 1 To 100
                        While (True)
                            bytesRead = ResponseStream1.Read(buff, 0, buff.Length)
                            If (bytesRead = 0) Then Exit While
                            FileStream.Write(buff, 0, bytesRead)
                            BackgroundWorker1.ReportProgress(Percentage)
                        End While
                    Next i
    The ReportProgress property is set to True but I need it to report more than once.

  6. #6
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    Re: I need a Progress Bar for my Download/Upload manager

    Okay, some questions.

    Where are you recalculating the value of Percentage? I don't see that in your code snippet. You have to recalculate the Percentage each iteration through the loop BEFORE you call ReportProgress(). This is probably the issue, i.e. it's calling ReportProgress more than once, but since it's always passing the same value, it doesn't LOOK that way.

    If that isn't it, then these questions become relevant.

    How big is the file you are downloading, and how big is the buffer you are using?

    Does it download the file successfully?

    Why do you have a 1 - 100 for loop? Are you trying to download the file 100 times?

    Finally, set breakpoints and step through it as it runs. Try and get an idea what is really happening.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width