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