I don’t get it. If I want to change the text on a button from a thread other than the UI thread in Visual Basic .NET, I need to use a delegate, and do something along the lines of
Private Delegate Sub SetTextDelegate(ByVal TheText As String)
Private Sub delSetText(ByVal TheText As String)
Button1.Text = TheText
End Sub
Private Sub ChangeText(ByVal TheText As String)
If Button1.InvokeRequired Then
Me.BeginInvoke(New SetTextDelegate(AddressOf delSetText), TheText)
Else
delSetText(TheText)
End If
End Sub
Of course I can make more generic functions that aren’t so hard-wired. But still, it seems like a lot of typing. Am I doing this in a roundabout way? How is this not included in the control properties—why would anyone leave this up to the programmer if it is required?
In C#, anonymous methods are very helpful here; perhaps there is something similar in VB? (my VB-fu is weak). You might also re-use the current method rather than have two; as an example:
Note that I’ve used
MethodInvokerhere deliberately – this is handled as a special case byInvoke, meaning it doesn’t have to use the (very slow)DynamicInvoke.I could also have done the
.Text = textin the anon method, but that seems to violate DRY.