We help IT Professionals succeed at work.

Another Parallel programming question  VB.NET

rutledgj
rutledgj used Ask the Experts™
on
So with your help I now have this working

  Dim w As New Work
            Parallel.ForEach(LstVendors, Sub(currentVendor)
                                             'Dim w As New Work
                                             w.DoWork(currentVendor)
                                         End Sub)

What I really need now is a way to :
1 Use a queue to process,
2. Have a cancellation token available
3. Have a way to know when all tasks are completed.

Please only vb.net code.  I can find csharp examples but have a very hard time converting them to vb.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
re-write your Work class a backgroundworker class and utilize the backgroundworker's cancellation and completed events to fulfill 2 and 3 above. here's a good Microsoft walthrough
http://msdn.microsoft.com/en-us/library/ywkkz4s1.aspx

Author

Commented:
I was looking more for a way to use the task.waitall method

Author

Commented:
It appears that waitall requires a list of task to monitor which I don't see a way to do that using the foreach loop. Is there another way to do this not using foreach where the tasks can be saved to a collection?
Commented:
here'sa code suggestion. I have used Object for your Work class, but a straight swap should be OK (where you have it defined). heres the inspiration: http://msdn.microsoft.com/en-us/library/dd270695.aspx
        Dim LstVendors As New List(Of Object)
        Dim vendorsACTION As Func(Of Object, Object) = Function(obj As Object)
                                                           'here I suppose you do your bits and bobs
                                                           Dim ReturnOBJ As Object = Nothing
                                                           Return ReturnOBJ
                                                       End Function

        Dim xTask As Task(Of Object)() = New Task(Of Object)(LstVendors.Count - 1) {}

        Parallel.ForEach(LstVendors, Sub(currentVendor)
                                         xTask(LstVendors.IndexOf(currentVendor)) = Task(Of Object).Factory.StartNew(vendorsACTION, LstVendors.IndexOf(currentVendor))
                                     End Sub)
        Task.WaitAll(xTask)

Open in new window

Author

Commented:
Ok. So what I have is a class Vendor.

I have a LstVendor(of Vendor)

I have another class called Worker with a method called DoWork(v as Vendor)

I'm not sure how to substitute my  items into your code where Object is.

Commented:
your class Worker is in this case replaced by the vendorsAction function. So I would just copy the DoWork method in there to the function.