I’m doing this:
Delegate Sub SetTextBoxText_Delegate(ByVal [Label] As TextBox, ByVal [text] As String)
' The delegates subroutine.
Public Sub SetTextBoxText_ThreadSafe(ByVal [Label] As TextBox, ByVal [text] As String)
' InvokeRequired required compares the thread ID of the calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true.
If [Label].InvokeRequired Then
MsgBox("invoke")
Dim MyDelegate As New SetTextBoxText_Delegate(AddressOf SetTextBoxText_ThreadSafe)
Me.Invoke(MyDelegate, New Object() {[Label], [text]})
Else
MsgBox("noinvoke")
[Label].Text = [text]
End If
End Sub
However it always uses noinvoke. If I try setting it normaly it gives me a thread-safe warning and doesn’t work. If I force invoke then it says the control isn’t created?
Could someone help?
It’s most likely because the control has not yet been created when you try to access it. Wait until the control has loaded, or check it using
Label.Created. Like so:Public Sub SetTextBoxText_ThreadSafe(ByVal Label As TextBox, ByVal text As String) If Label.Created Then If Label.InvokeRequired Then MsgBox("invoke") Dim MyDelegate As New SetTextBoxText_Delegate(AddressOf SetTextBoxText_ThreadSafe) Me.Invoke(MyDelegate, New Object() {Label, text}) Else MsgBox("noinvoke") Label.Text = text End If End If End SubP.S. You don’t need a custom delegate type, just use
Action(Of TextBox, String). You also don’t need square brackets aroundLabelortext.