Page 3 of 3 FirstFirst 123
Results 81 to 91 of 91

Thread: Using the BackgroundWorker Component

  1. #81
    New Member
    Join Date
    Nov 2012
    Posts
    5

    Re: Using the BackgroundWorker Component

    Code:
    BackgroundWorker1_DoWork
            Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
            Dim lines As String() = FileToArray(My.Computer.FileSystem.CurrentDirectory & "/filelist.txt")
            Dim line As String
            For Each line In lines
                uriSource = New Uri("http://localhost/" & line)
                uriPath = My.Computer.FileSystem.CurrentDirectory & line
                ???For i As Integer = 1 To 100
                    worker.ReportProgress(i, "Downloading : " & line)
                ??????Next i
                downloading.DownloadFile(uriSource, uriPath)
            Next line
    ----------------
        Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
            Me.ProgressBar1.Value = e.ProgressPercentage
            Me.Label6.Text = TryCast(e.UserState, String)
        End Sub
    i Would like to know how to make progress bar work with my "for each" line reader for downloading. for example how should i get BytesReceived .
    or like
    Code:
             
    ProgressBar1.Maximum = e.TotalBytesToReceive
    ProgressBar1.Value = e.BytesReceived
    thanks in advance!!

  2. #82
    New Member
    Join Date
    Nov 2012
    Posts
    5

    Re: Using the BackgroundWorker Component

    sry for duplicate post website lagged
    Last edited by Tompkins; Nov 27th, 2012 at 06:43 AM. Reason: duplicate post :/

  3. #83

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Using the BackgroundWorker Component

    Quote Originally Posted by Tompkins View Post
    Code:
    BackgroundWorker1_DoWork
            Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
            Dim lines As String() = FileToArray(My.Computer.FileSystem.CurrentDirectory & "/filelist.txt")
            Dim line As String
            For Each line In lines
                uriSource = New Uri("http://localhost/" & line)
                uriPath = My.Computer.FileSystem.CurrentDirectory & line
                ???For i As Integer = 1 To 100
                    worker.ReportProgress(i, "Downloading : " & line)
                ??????Next i
                downloading.DownloadFile(uriSource, uriPath)
            Next line
    ----------------
        Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
            Me.ProgressBar1.Value = e.ProgressPercentage
            Me.Label6.Text = TryCast(e.UserState, String)
        End Sub
    i Would like to know how to make progress bar work with my "for each" line reader for downloading. for example how should i get BytesReceived .
    or like
    Code:
             
    ProgressBar1.Maximum = e.TotalBytesToReceive
    ProgressBar1.Value = e.BytesReceived
    thanks in advance!!
    There are various options but the simplest is to just look at the name of the first parameter of the ReportProgress method: percentProgress. Instead of setting the Maximum of the ProgressBar to the total number of bytes and the Value to the actual number of bytes, simply leave the Maximum as the default 100 and calculate the percentage yourself and pass that one value, e.g. if you have 5,000,000 bytes to download and you've downloaded 2,000,000 then the percentage is 40% so that's what you pass to ReportProgress.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #84
    New Member
    Join Date
    Nov 2012
    Posts
    5

    Re: Using the BackgroundWorker Component

    Quote Originally Posted by jmcilhinney View Post
    There are various options but the simplest is to just look at the name of the first parameter of the ReportProgress method: percentProgress. Instead of setting the Maximum of the ProgressBar to the total number of bytes and the Value to the actual number of bytes, simply leave the Maximum as the default 100 and calculate the percentage yourself and pass that one value, e.g. if you have 5,000,000 bytes to download and you've downloaded 2,000,000 then the percentage is 40% so that's what you pass to ReportProgress.
    How this should look like? at which point i should send that percentage with reportprogress in for each loop. can you show me some sample? Thanks for response!

  5. #85

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Using the BackgroundWorker Component

    Quote Originally Posted by Tompkins View Post
    How this should look like? at which point i should send that percentage with reportprogress in for each loop. can you show me some sample? Thanks for response!
    Anyone who has done primary school maths knows how to calculate a percentage so I'm not going to provide an example of that. As for where to call ReportProgress, just ask yourself where in your code you want to update the ProgressBar and that's where you call ReportProgress.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #86
    New Member
    Join Date
    Nov 2012
    Posts
    5

    Re: Using the BackgroundWorker Component

    Quote Originally Posted by jmcilhinney View Post
    Anyone who has done primary school maths knows how to calculate a percentage so I'm not going to provide an example of that. As for where to call ReportProgress, just ask yourself where in your code you want to update the ProgressBar and that's where you call ReportProgress.
    I want to update ProgressBar while downloading each file. For example its downloading one file then the progressbar fillsup, and then when it starts to download other file progress bar starts from begining just for different file size. As i can understand you are telling me the example for overall files being downloaded. Sorry for my bad english .. And thanks for your patience and help!
    Code:
            Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
            Dim lines As String() = FileToArray(My.Computer.FileSystem.CurrentDirectory & "/filelist.txt")
            Dim line As String
            Dim j As Integer
            Dim percent As Short
            For Each size In sizes
                Dim i As Integer = Convert.ToInt32(size)
                j = j + i
            Next size
            For Each line In lines
                Console.WriteLine(line)
                uriSource = New Uri("http://localhost/" & line)
                uriPath = My.Computer.FileSystem.CurrentDirectory & line
                worker.ReportProgress(percent, "Downloading : " & line)
                downloading.DownloadFile(uriSource, uriPath)
                Dim info As New FileInfo(My.Computer.FileSystem.CurrentDirectory & line)
                Dim length As Long = info.Length
                Dim g As Long
                g = g + length
                percent = (g * 100) / j
            Next line
    this is for overall. it works, but what about each file Thank You!
    Last edited by Tompkins; Dec 3rd, 2012 at 08:22 AM. Reason: added some information

  7. #87
    New Member
    Join Date
    Feb 2013
    Posts
    7

    Re: Using the BackgroundWorker Component

    Hi John,
    Thanks for useful explanation.
    What about showing a message in another form, such as a none BorderStyleForm with a pic on it saying "please wait" or "loading" during the progress and closing the form after completion?

  8. #88
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Using the BackgroundWorker Component

    With so many current BGW threads i think this should be on page One.
    My Github - 1d3nt

  9. #89
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Using the BackgroundWorker Component

    Cross thread operation error...


    Ident, you should know better than that... you know you can't access the UIThread from the BGW directly... it has to be invoked and passed back to the main UI processing thread... THEN you can get it to show up on the main form (Page1)...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #90
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Using the BackgroundWorker Component

    Took me a while to catch that one...has me looking on page 1 for ident's post rofl
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  11. #91
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Using the BackgroundWorker Component

    Quote Originally Posted by Niya View Post
    Took me a while to catch that one...has me looking on page 1 for ident's post rofl
    90% of me says techgnome is making a joke "thread" related, the rest is left a little confused if not
    My Github - 1d3nt

Page 3 of 3 FirstFirst 123

Posting Permissions

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



Click Here to Expand Forum to Full Width