Vb.net Parallel foreach problem

rutledgj
rutledgj used Ask the Experts™
on
I'm trying to do a Parallel.ForEach and can't figure out the correct syntax.

I have a list of Vendor objects (lstVendors).
I have a class (UpdateThread) that contains the function UpdateData(v as Vendor)

I want to do a Parallel.ForEach(lstVendors,  


Don't know where to go next. Any help on completing the for loop code?

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Is UpdateData a Shared method?

Author

Commented:
it is a public method in another class

Commented:
try this where currentVendor is one of the lstVendors
yu can see here too
Writting simple parallel for.each loops
Parallel.ForEach(lstVendors, Sub(currentVendor)


                                              End Sub)

Open in new window

Author

Commented:
Ok but how do i call the UpdateData method in this example?
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
I think you have something like this:

Dim ut As New UpdateThread()
Dim lstVendor As New List(Of Vendor)

Parallel.ForEach(lstVendor, New Action(Of Vendor)(AddressOf ut.UpdateData))

Open in new window


Please correct me if the instances displayed above are not correct.
Commented:
Dim NewX as New UpdateThread
Parallel.ForEach(lstVendors, Sub(currentVendor)
                                            NewX.UpdateData(currentVendor)
                                         End Sub)
Commented:
I'll have to check on this tomorrow. Thanks guys

Commented:
Actually, you will need to declare the new class inside the loop, aka
Parallel.ForEach(lstVendors, Sub(currentVendor)
                                    Dim NewX as New UpdateThread
                                    NewX.UpdateData(currentVendor)
                              End Sub)

Open in new window

Shahan AyyubSenior Software Engineer

Commented:
Hi!

This will help you in finding the syntax of Parallel Foreach:
http://www.c-sharpcorner.com/UploadFile/vendettamit/8573/
Shahan AyyubSenior Software Engineer

Commented:
There is a correction at the end of the article mention in the link: Please have a look at that also.

Author

Commented:
Both of these methods worked and you don't have to create a new instance of UpdateThread  in the loop. Just one instance will work.

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