我正在编写一个同时运行多个线程的应用程序. 每个线程与Web服务器通信并下载不同数量的数据. 我想显示应用程序中所有线程的总进度. 每个线程每下载1兆字节就会引发一个事件: R
每个线程与Web服务器通信并下载不同数量的数据.
我想显示应用程序中所有线程的总进度.
每个线程每下载1兆字节就会引发一个事件:
RaiseEvent My_Event(Size_Downloaded as double,Total_Size as double)
有没有办法显示主窗体中下载的总数而不会出现跨线程错误?
它不一定是瞬间的,所以计时器可能有效吗?
您可以更改事件签名并添加线程的ID并使用计时器显示节目总数
Dim _info As New ConcurrentDictionary(Of Integer, DLoadInfo) Sub MyEvent(id As Long, Size_Downloaded As Double, Total_Size As Double) Dim v = New DLoadInfo() With { .SizeDownloaded = Size_Downloaded, .TotalSize = Total_Size } _info.AddOrUpdate(id, v, Function(key, oldValue) Return v End Function ) End Sub Private Sub TimerDisplay_Tick(sender As Object, e As EventArgs) Dim sizeDownloaded, totalSize As double For Each o As DLoadInfo In _info.Values sizeDownloaded += o.SizeDownloaded totalSize += o.TotalSize Next TextBoxSizeDownloaded.Text = sizeDownloaded TextBoxTotalSize.Text = totalSize End Sub Class DLoadInfo Public property SizeDownloaded As Double Public property TotalSize As Double End Class