Using the Parallel.ForEach to Speedup the execution of Loops

raklali 236 Reputation points
2022-01-22T14:57:57.757+00:00

Can the Parallel.ForEach Loop be used on values other than DataTables?
I attempted the following, but the program execution is hanging

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
        Parallel.ForEach(Segments.OfType(Of Segment),
                          Sub(segment)
                              Try
                                   segment.DrawLine(e.Graphics)
                              Catch ex As Exception
                              End Try
                          End Sub
                          )
   End Sub
Developer technologies | VB
0 comments No comments
{count} votes

Answer accepted by question author
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2022-01-22T17:58:28.8+00:00

    Hi,
    If you use e.Graphics inside segment.Drawline you can get race conditions because it's impossible to use graphics in parallel. To prevent race conditions use synlock like in following demo:

    Public Class Form32
    
      Private WithEvents PictureBoxRadarScope As New PictureBox With {.Dock = DockStyle.Fill}
      Private Segments As New List(Of Segment)
      Private Sub Form32_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Controls.Add(PictureBoxRadarScope)
        Dim rnd As New Random
        For i = 1 To 1000
          Segments.Add(New Segment With {.ID = i, .P1 = New Point(rnd.Next(10, 1000), rnd.Next(10, 1000)), .P2 = New Point(rnd.Next(10, 1000), rnd.Next(10, 1000))})
        Next
      End Sub
    
      Private Sub PictureBoxRadarScope_Paint(sender As Object, e As PaintEventArgs) Handles PictureBoxRadarScope.Paint
        Parallel.ForEach(Segments.OfType(Of Segment),
                      Sub(segment)
                        Try
                          segment.DrawLine(e.Graphics)
                        Catch ex As Exception
                          Debug.Print($"{segment.ID} - {ex.Message}")
                        End Try
                      End Sub
                      )
      End Sub
    
      Public Class Segment
        Public Sub DrawLine(gr As Graphics)
          SyncLock gr
            gr.DrawLine(Pens.Black, P1, P2)
          End SyncLock
        End Sub
        Public Property ID As Integer
        Public Property P1 As Point
        Public Property P2 As Point
      End Class
    
    End Class
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.