Results 1 to 3 of 3

Thread: BGWorker woes.. How to loop a list?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2007
    Posts
    520

    BGWorker woes.. How to loop a list?

    Hey guys,

    So, i have a list that has things that needs to be looped. This list can be up to 500k items.


    I would like to add a multithreading option with a variable. This is not an issue, as I know how to make multiple bgw's (Code below)

    Code:
                   i = spin_NumOfThreads.Value
            Dim bgwrks(i) As System.ComponentModel.BackgroundWorker
    
     For Each bgworker As System.ComponentModel.BackgroundWorker In bgwrks
                bgworker = New System.ComponentModel.BackgroundWorker
                AddHandler bgworker.DoWork, AddressOf myDoWork
                AddHandler bgworker.ProgressChanged, AddressOf reportprogress
                bgworker.WorkerReportsProgress = True
                bgworker.RunWorkerAsync()
            Next
    so in that code, it'll make the number of threads based off of my spineditor.

    Now, ideally i'd like it to go through a list, each thread does its thing.

    Now, a simple dowork would just allow it to do 1.

    i would ideally like it to stay @ X threads until the list is done, so how would I basically go about remaking a thread once it finishes to continue?


    So, lets say I have 50 items in a list, i set the number of threads to 5.

    The first 5 would do the first 5 items, when the 1st one finishes, it works on item 6, then the next would do 7, etc. and it'd keep remaking until 50 is done.


    I'm pretty stuck on the logic of how to go about doing this, and would appreciate some insight from you guys. Thanks!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: BGWorker woes.. How to loop a list?

    Follow the CodeBank link in my signature and check out my BackgroundMultiWorker thread. Just loop through your list and use each item as a token to create a task. That BackgroundMultiWorker class uses the BackgroundWorker class internally, which uses the ThreadPool class internally. The ThreadPool class will handle scheduling for you, i.e. it will run an appropriate number of tasks simultaneously and queue the rest until resources become available.

    Alternatively, you could look at using the new .NET 4.0 Tasks library. I haven't really investigated it myself yet though, so I can't go into specifics other than to say that it is the preferred way to implement multi-threading in .NET 4.0.
    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

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2007
    Posts
    520

    Re: BGWorker woes.. How to loop a list?

    Thanks buddy! I'll look into that!

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