![]() |
Click here to advertise with us
|
|
|||||||
| Windows Forms Discussion related to Winforms application development |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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
Thank you. |
|
|||
|
How To Update Controls Using BackgroundWorker in VB.NET
This article was insightful and simple to follow when I was learning about the BackgroundWorker. |
|
|||
|
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!
|
|
||||
|
Quote:
Quote:
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.
__________________
Some useful links: Learning videoes, Code Samples, WMI Code Creator, MSDN, The Code Project, WindowsClient.net, ASP.net, W3 Schools, Regular-Expressions.info, GDI+ FAQ
How to format posts with code blocks etc - present the problem/post properly ![]() |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|