Share via

Using Only One ForEach Loop

raklali 236 Reputation points
2022-02-02T17:20:50.243+00:00

Please see the following Code:-

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
                            )

         Parallel.ForEach(Ellipses.OfType(Of Ellipse),
                            Sub(ellipse)
                                Try
                                     ellipse.DrawEllipse(e.Graphics)
                                Catch ex As Exception
                                End Try
                            End Sub
                            )
     End Sub

Is it Possible to use only One ForEach loop instead of Two as shown above? If No, is there a way to Enhance the Speed of Execution by using async and await?

Developer technologies | VB

Answer accepted by question author

Peter Fleischer (former MVP) 19,351 Reputation points
2022-02-03T04:32:42.163+00:00

Hi,
you can use base class, like this:

  Private ToDraw As New List(Of BaseClass)

...

  Private Sub PictureBoxRadarScope_Paint(sender As Object, e As PaintEventArgs) Handles PictureBoxRadarScope.Paint
    Parallel.ForEach(ToDraw.OfType(Of BaseClass),
                  Sub(element)
                    Try
                      element.Draw(e.Graphics)
                    Catch ex As Exception
                      Debug.Print($"{element.ID} - {ex.Message}")
                    End Try
                  End Sub
                  )
  End Sub

  Public MustInherit Class BaseClass
    MustOverride Sub Draw(gr As Graphics)
    Public Property ID As Integer
  End Class

  Public Class Segment
    Inherits BaseClass

    Dim arr(10000) As Double
    Public Overrides Sub Draw(gr As Graphics)
      For i = 0 To arr.GetUpperBound(0)
        arr(i) = i ^ 3
      Next
      SyncLock gr
        gr.DrawLine(Pens.Black, P1, P2)
      End SyncLock
    End Sub

    Public Property P1 As Point
    Public Property P2 As Point
  End Class

  Public Class Ellipse
    Inherits BaseClass

    Dim arr(10000) As Double
    Public Overrides Sub Draw(gr As Graphics)
      For i = 0 To arr.GetUpperBound(0)
        arr(i) = i ^ 3
      Next
      SyncLock gr
        gr.DrawEllipse(Pens.Black, P.X, P.Y, W, H)
      End SyncLock
    End Sub

    Public Property P As Point
    Public Property W As Single
    Public Property H As Single
  End Class

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

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.