confused with threads - in order to refresh progressbar

DanielFL

Member
Joined
Feb 15, 2009
Messages
5
Programming Experience
3-5
Hello. I spent couple days reading threads in vb.net but still I am so confused and none of examples work. Would you please help me get the example below to work in a thread - it download the file from internet but showing download progress. Obviously it does not work in thread so I dont see progressbar being refreshed at all.
VB.NET:
 Private Function DownloadChunks(ByVal sURL As String, ByVal pProgress As ProgressBar, ByVal Filename As String)

        Dim URLReq As HttpWebRequest
        Dim URLRes As HttpWebResponse
        Dim FileStreamer As New FileStream(Filename, FileMode.Create)
        Dim bBuffer(999) As Byte
        Dim iBytesRead As Integer

        Try
            URLReq = WebRequest.Create(sURL)
            URLRes = URLReq.GetResponse
            Dim sChunks As Stream = URLReq.GetResponse.GetResponseStream
            pProgress.Maximum = URLRes.ContentLength

            Do
                iBytesRead = sChunks.Read(bBuffer, 0, 1000)
                FileStreamer.Write(bBuffer, 0, iBytesRead)
                If pProgress.Value + iBytesRead <= pProgress.Maximum Then
                    pProgress.Value += iBytesRead
                Else
                    pProgress.Value = pProgress.Maximum
                End If

                Thread.Sleep(100)

            Loop Until iBytesRead = 0
            pProgress.Value = pProgress.Maximum
            sChunks.Close()
            FileStreamer.Close()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
Any help greatly appreciated.
Thank you.
 
The progress bar needs to be on the UI thread (the form) and you can use a BackgroundWorker to do the downloading of the files. BW's can report progress via an event and you use that to pass the percentage from the background thread to the UI thread
 
I almost work...

that article was very interestign and I made significiant progress.
I am, hovere stuck at the end.
I made my download working with System.ComponentModel.BackgroundWorker, however because I know in the future I want to have more workers, while firing RunWorkerAsync(param) I am passing an Array Object in param. this way I can have different tasks run through a worker.
So when I do _DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) i can switch through e.Argument(0) and look which task to do... then all necessary variables I can pass through that object like lets say Dim Filename As String = e.Argument(1), Dim sURL As String = e.Argument(2)

There is not a problem with checking state for different thread through _ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) because again I can do switch around e.UserState(0) (that matches e.Argument(0) ), however when it comes to the final step -- _RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles TestWorker.RunWorkerCompleted - the e.Argument(0) is not there, neither is e.UserState(0).

Question: how can I distinquish which action is completed if I want to use Worker for more than one task i presented here model where passed Variable is an Array?

thank you for your helpful help! :)
 
DanielFL said:
how can I distinquish which action is completed if I want to use Worker for more than one task
From help:
If your operation produces a result, you can assign the result to the DoWorkEventArgs.Result property. This will be available to the RunWorkerCompleted event handler in the RunWorkerCompletedEventArgs.Result property.
The Result is just another "userstate".

When passing multiple work items to a BW I sometimes use ReportProgress to notify "completed" progress for individual items, this method happens to have a "userstate" available also where you can pass any info necessary. The RunWorkerCompleted event would then be just some kind of "finished processing all" notification.
 
Back
Top