Hi, Whats the best way to handle updating the UI lots of times from a worker thread. Depending on speed the UI may need updating 3/4 times in a second. Say if the overall task takes 1 minute to complete, we could have over 100 updates in that given time.
My problem is it feels like i am creating a worker thread just to constantly jump back to the UI thread.
do you think the SynchronizationContext class would be better suited then what i am doing below.
vb Code:
Public Class MainForm Private Event SomeThingChnaged As EventHandler(Of SomeThingChnagedEventArghs) Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartButton.Click Dim t As New System.Threading.Thread(AddressOf DoWork) t.Start() End Sub Private Sub DoWork() ' Do work RaiseEvent SomeThingChnaged(Me, New SomeThingChnagedEventArghs("Data")) 'do work RaiseEvent SomeThingChnaged(Me, New SomeThingChnagedEventArghs("Data")) ' and so on End Sub Private Sub UpdateInterface(ByVal sender As Object, ByVal e As EventArgs) Handles Me.SomeThingChnaged If Me.StatusTextBox.InvokeRequired Then Me.StatusTextBox.Invoke(New Action(Of Object, EventArgs)(AddressOf UpdateInterface), sender, e) Else Dim value As String = DirectCast(sender, SomeThingChnagedEventArghs).Value Me.StatusTextBox.AppendText(value & vbNewLine) End If End Sub End Class




Reply With Quote