John mention's we should avoid calling the middle man if we know the UI thread needs access. How ever would it be more suited if we wanted to update more then one control to either invoke the control "If Me.Control.InvokeRequired" or invoke each control individually. Code to make things a little clearer.
We always know the control needs invoking.
Accessing each control using the method invoker.
Invoking only once
which is more suited? Invoke each control separate? Or invoking once. Feels like repeated code using the first method. But then seems more logical since we know the UI thread needs invoking.
We always know the control needs invoking.
Accessing each control using the method invoker.
vb Code:
Public Class MainForm Private WithEvents m_someWork As New SomeWork Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim t As New Threading.Thread(AddressOf Me.m_someWork.DoWork) t.Start() End Sub Private Sub WorkCompleted(ByVal sender As Object, ByVal e As EventArgs) Handles m_someWork.SomeValue Dim value As String = DirectCast(e, SomeValueEventArghs).Value Me.Label1.Invoke(New MethodInvoker(Sub() Me.Label1.Text = value)) Me.Label2.Invoke(New MethodInvoker(Sub() Me.Label2.Text = value)) End Sub End Class
Invoking only once
vb Code:
Public Class MainForm Private WithEvents m_someWork As New SomeWork Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim t As New Threading.Thread(AddressOf Me.m_someWork.DoWork) t.Start() End Sub Private Sub WorkCompleted(ByVal sender As Object, ByVal e As EventArgs) Handles m_someWork.SomeValue If Me.Label1.InvokeRequired Then Me.Label1.Invoke(New Action(Of Object, EventArgs)(AddressOf WorkCompleted), sender, e) Else Dim value As String = DirectCast(e, SomeValueEventArghs).Value Me.Label1.Text = value Me.Label2.Text = value End If End Sub End Class
which is more suited? Invoke each control separate? Or invoking once. Feels like repeated code using the first method. But then seems more logical since we know the UI thread needs invoking.