I'm trying to work out the best way to keep my UI from locking up when updating a DataGridView
in a multithreaded app.
The app is polling ~100 machines for data and updating the DGV with the results every few seconds.
This is very busy and is locking up the UI making any interaction sluggish. What is the most efficient way to update the UI in this scenario?
Current setup is that DGV is linked to MySQL table with a TableAdapter
and UI delegate sub updates row in the TA.
Thread spawning sub:
Private Sub PollEDMs()
For i As Integer = 0 To Me.DataGrid.Rows.Count - 1
Dim edm As New EDM(Me.DataGrid.Rows(i).Cells("IP").Value)
Try
Dim thPoll As New Thread(AddressOf edm.CollectData)
thPoll.Name = "thread" & i
thPoll.IsBackground = True
thPoll.Priority = ThreadPriority.BelowNormal
thPoll.Start()
Interlocked.Increment(ThrCount)
Me.ToolStripThreadStatusLabel1.Text = "Active Threads: " & ThrCount
Catch ex As Exception
logger.Log(LogLevel.Error, edm.IP & " => " & ex.Message)
End Try
Next
End Sub
Invoked Update Sub (Called from EDM class running on another thread)
Public Sub EDMEthernetUpdate(ByVal EDM As EDMEthernet)
Try
SyncLock threadLock
Me.EDMTableAdapter.UpdateByIP(EDM.Ping, EDM.Status.Message,
EDM.Timer.ElapsedMilliseconds, EDM.IP)
End SyncLock
logger.Log(LogLevel.Info, EDM.IP & " => Updated")
Catch ex As Exception
logger.Log(LogLevel.Error, EDM.IP & " => " & ex.Message)
End Try
End Sub