parallel programming question

rutledgj
rutledgj used Ask the Experts™
on
I want to use the for each parallel method to cycle through a list of objects and perform some work on them.

The list can contain 100 objects but can grow larger so I want the foreach loop to only work on say 5-10 objects at a time, wait for those threads to finish and do the next 5.

Could anyone show me code as to how I would accomplish this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
Here is an example


Dim partitioner = New ThreadingHelper.ChunkPartitioner(targets)
Dim parallelOptions = New ParallelOptions() With { Key .MaxDegreeOfParallelism = 10 }
Parallel.ForEach(partitioner, parallelOptions, Function(s) DoWP(projtorun, s))

http://www.aspcode.net/ParallelForEach-limit-the-number-of-threads.aspx

Author

Commented:
I guess I'm not really clear on how this works. Lets say I have a List of Vendors (List of T) and want to cycle through all of them.  How would this example work with that list    (lstVendors)?
Most Valuable Expert 2012
Top Expert 2014

Commented:
Show me your current code
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Commented:
Here is the method signature for a parallel for each:
Parallel.ForEach(IEnumerable<TSource> source, Action<TSource> body)
1. In your case, to set off the method to iterate through ALL themembers in your List(Of Vendors), you'd do:
        Dim MyList As New List(Of Vendor)
        Parallel.ForEach(MyList, Sub(x)
                                     'here your variable is x
                                 End Sub)

Open in new window

2. To fire off only a batch at a time, you'd"probably" have to enclose the for each inside a while, and have a counter that logs the completion of each parallel task.

Look at the links below for some "invaluable" walk-through from Microsoft on the subject.
a) For simple ones:http://msdn.microsoft.com/en-us/library/dd460720.aspx
b) For threaded ones:http://msdn.microsoft.com/en-us/library/dd997393.aspx

Author

Commented:
Ok so this is what I need to do.  I have a class that will do the work on the vendor in the list. so in the foreach loop I need to dim an instance of the class, pass the class the org from the list and do the work. How do I get the org out of the list in the foreach loop?

Parallel.ForEach(LstVendors, Sub(x)
                                Dim pu as new VendorUpdateThread
                                        pu.vendor = LstVendors(x)            'this doesn't work
                                        pu.start
                                       End Sub)

Author

Commented:
Ok. I see now that x is the variable. So I have this

 Parallel.ForEach(LstVendors, Sub(i)
                                         Dim pu As New PatientUpdateThread
                                         pu.MyVendor = i
                                         pu.UpdatePatients()
                                     End Sub)

It works except I have 20 vendors in the list. It does 4 of them and then exists the loop.

Commented:
Have you looked at the  walkthroughs I gave you?

Where are your 20 vendors? In LstVendors or are they initialised somewhere else in your PatientUpdateThread?

We only have the snapshot of code that you have given here, and can not perform miracles on code we do not see. The parallel for each walkthrough's should be adequate guidance for that part of your query, however, you'll need to elaborate more on the resultant query ....

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial