2

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
doovers
  • 8,545
  • 10
  • 42
  • 70
  • If you are doing all your processing on the UI thread, then have a look at this post: http://stackoverflow.com/questions/1644874/prevent-ui-from-freezing-without-additional-threads – Mike Hixson Jul 02 '14 at 05:55
  • Perhaps you could have your threads writing their data into some shared data-structure, then have a separate timer for the DGV to update all its rows from that structure once every second. Currently you're updating the DGV 100 times per second, so it's not surprising that it's locking up. – Blorgbeard Jul 06 '14 at 21:53
  • @Blorgbeard Thanks for the suggestion. I gave that a try and it improves the situation but unfortunately still leaves a lot to be desired as far as user experience is concerned. For example, When scrolling the DGV, the UI locks up. – doovers Jul 07 '14 at 02:11

1 Answers1

0

Manually dispatch the work to a new thread or use the TPL (async/await). Post your code if you need further assistance .

Have a look at how to create and terminate threads or the TPL

  • All the work is done on other threads it just the table adapter updates that are locking the UI. I've posted some code in my question which might clarify whats going on. – doovers Jul 02 '14 at 07:31
  • Oh gosh, I don't know any VB.Net but I think I now understand your problem. I had a similar problem when your drop downs were loading thousands of result sets. The only route we used to solve this was to change them to use ajax calls so that the rest of the UI remained responsive. Perhaps someone with VB.Net experience would be able to help you further. – Praval 'Shaun' Tirubeni Jul 02 '14 at 09:40