I'm writing an application that has multiple threads running simultaneously. Each thread talks to a web-sever and downloads different amounts of data. I would like to display the total progress for all the threads in the application.
Each thread raises an event every 1 megabyte downloaded:
RaiseEvent My_Event(Size_Downloaded as double,Total_Size as double)
Is there a way to display the total downloaded in the main form without getting cross thread errors?
it doesn't have to be instantaneous, so a timer might work?
You can change event signature and add id of the Thread and use timer to display show total
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With