none
Parallel.ForEach (VB.NET)

    Question

  • Hi I need little help to convert below for each code into the parallel.Foreach code.Below code is in VB.Net

     

    For Each printerName As DataRow In

    printerNamesTable.Rows

     

    Dim objName As String

    = printerName(0).ToString()

     

    If ObjHolder.ContainsKey(objName) = True

    Then 'ObjHolder is a dictionary

    ObjHolder(objName).StartMonitoring(objName)

     

    End

    If

     

    Next

     

    Thursday, May 05, 2011 10:52 AM

Answers

  • No, it takes rows as arguament. you can try like below. It works for me. Cheers

     

    Dim myoptions As New ParallelOptions With {.CancellationToken = cts.Token} ' optional to cancel process
    
    
      Parallel.ForEach(printerNamesTable, myoptions, Sub(myRow As printerNamesTableDS.Row, loopstate As ParallelLoopState)
       End Sub)
    

     


    "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
    Monday, May 23, 2011 10:16 AM

All replies

  • Here you go..

     

    Parallel.ForEach(printerNamesTable.Rows, Function(printerName)

    Dim objName As String = printerName(0).ToString()

    If ObjHolder.ContainsKey(objName) = True
     Then 'ObjHolder is a dictionary

    ObjHolder(objName).StartMonitoring(objName)
    End If

    End Function)

     I just did a mental conversion from C# to VB. But I guess you get the idea.

    Also, go through this thread for some more insights...

    http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/244fb290-9b0d-47e2-91cf-982eb6f99637/

     


    Thursday, May 05, 2011 9:30 PM
  • Hi Saurabh,

    I have tried code provided by you but .Net shows following error

    "Overload resolution failed because no accessible 'FoeEach' can be called with these arguments".

     

     I have tried below solution and it works(with one exception as "Function <anonymous method> doesn't return a value on all code paths.A null reference exception could occur at run time when result is used.")

     

     

    Dim l As List(Of String) = New List(Of String

    )

     

    For Each printerName As DataRow In

    printerNamesTable.Rows

    l.Add(printerName(0).ToString())

     

    Next

     

    Parallel.ForEach(l, Function

    (printerName)

     

    If ObjHolder.ContainsKey(printerName) = True

    Then

    ObjHolder(printerName).StartMonitoring(printerName)

     

    End

    If

     

    End Function

    )


    Tuesday, May 10, 2011 11:20 AM
  • Hi,

    I think parallel.Foreach not takes table rows as argument.

    I have solved the issue with the use of following code.

     

     

    For Each printerName As DataRow In

    printerNamesTable.Rows

    l.Add(printerName(0).ToString())

     

    Next

     

    Parallel

     

     

    .ForEach(l, Sub

    (printerName)

     

     

    If ObjHolder.ContainsKey(printerName) = True

    Then

    ObjHolder(printerName).StartMonitoring(printerName)

     

     

    End

    If

     

     

    End Sub

    )

     

     
    Wednesday, May 11, 2011 1:31 PM
  • No, it takes rows as arguament. you can try like below. It works for me. Cheers

     

    Dim myoptions As New ParallelOptions With {.CancellationToken = cts.Token} ' optional to cancel process
    
    
      Parallel.ForEach(printerNamesTable, myoptions, Sub(myRow As printerNamesTableDS.Row, loopstate As ParallelLoopState)
       End Sub)
    

     


    "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
    Monday, May 23, 2011 10:16 AM