Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asynchronous events being fired

Tags:

vb.net

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?

like image 332
Frank_Vr Avatar asked Feb 09 '23 10:02

Frank_Vr


1 Answers

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
like image 53
volody Avatar answered Feb 15 '23 09:02

volody