Developer technologies | VB
An object-oriented programming language developed by Microsoft that can be used in .NET.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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?
An object-oriented programming language developed by Microsoft that can be used in .NET.
Answer accepted by question author
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