View Single Post
  #1 (permalink)  
Old 02-17-2009, 1:05 PM
DanielFL DanielFL is offline
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Feb 2009
Posts: 5
Reputation: 0
DanielFL is on a distinguished programming path ahead
Default 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.
Reply With Quote