Visual Basic .NET Forums  
Click here to advertise with us

Go Back   Visual Basic .NET Forums > VB.NET > Windows Forms

Windows Forms Discussion related to Winforms application development

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-17-2009, 1:05 PM
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-17-2009, 1:17 PM
JuggaloBrotha's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Jun 2004
Location: Lansing, MI; USA
Age: 25
Posts: 3,635
Reputation: 396
JuggaloBrotha master of VB.NETJuggaloBrotha master of VB.NETJuggaloBrotha master of VB.NETJuggaloBrotha master of VB.NETJuggaloBrotha master of VB.NETJuggaloBrotha master of VB.NETJuggaloBrotha master of VB.NETJuggaloBrotha master of VB.NETJuggaloBrotha master of VB.NET
Default

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 2005 & 2008 Pro w/sp1 on Win7 Ultimate x64.


There are 3 kinds of people in the world: Those who can count and those who can't.
4 out of 3 people have trouble with fractions.

Windows has a 64 bit GUI for a set of 32 bit extensions on a 16 bit shell for an 8 bit OS using a 4 bit kernel made by a 2 bit company that can't stand 1 bit of competition.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-17-2009, 1:22 PM
VB.NET Forum Idol
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Feb 2008
Location: USA
Posts: 877
Reputation: 500
MattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond repute
Default

How To Update Controls Using BackgroundWorker in VB.NET

This article was insightful and simple to follow when I was learning about the BackgroundWorker.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-17-2009, 3:13 PM
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
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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-17-2009, 6:54 PM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 37
Posts: 10,328
Reputation: 1315
JohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond repute
Default

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:
Quote:
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.
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-17-2009, 7:15 PM
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

thank you very much!! now it works for me as well
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 3:12 PM.

Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2


For advertising opportunities click here.