gpt4 book ai didi

.net - 在繁忙的多线程应用程序中更新 UI 的最佳方式

转载 作者:行者123 更新时间:2023-12-03 13:12:39 27 4
gpt4 key购买 nike

我正在尝试找出在更新 DataGridView 时防止我的 UI 锁定的最佳方法。在多线程应用程序中。

该应用程序正在轮询约 100 台机器的数据,并每隔几秒用结果更新 DGV。

这很忙,并且锁定了 UI,使任何交互都变得迟缓。在这种情况下更新 UI 的最有效方法是什么?

当前设置是 DGV 通过 TableAdapter 链接到 MySQL 表。和 UI 委托(delegate)子更新 TA 中的行。

线程生成子:

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

调用的更新子(从另一个线程上运行的 EDM 类调用)
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

最佳答案

手动将工作分派(dispatch)到新线程或使用 TPL (async/await)。如果您需要进一步的帮助,请发布您的代码。

看看如何create and terminate threadsTPL

关于.net - 在繁忙的多线程应用程序中更新 UI 的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24522335/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com