Results 1 to 6 of 6

Thread: confused with threads - in order to refresh progressbar

  1. #1
    DanielFL is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Feb 2009
    Posts
    5
    Reputation
    0

    confused with threads - in order to refresh progressbar

    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.
    Code:
     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.

  2. #2
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,216
    Reputation
    654
    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
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  3. #3
    MattP is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    WY, USA
    Posts
    1,207
    Reputation
    559
    How To Update Controls Using BackgroundWorker in VB.NET

    This article was insightful and simple to follow when I was learning about the BackgroundWorker.

  4. #4
    DanielFL is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Feb 2009
    Posts
    5
    Reputation
    0

    Lightbulb 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!

  5. #5
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    13,315
    Reputation
    1991
    Quote Originally Posted by DanielFL
    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.

  6. #6
    DanielFL is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Feb 2009
    Posts
    5
    Reputation
    0
    thank you very much!! now it works for me as well

Bookmarks

Posting Permissions

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

Search Engine Optimization by vBSEO