InvokeRequired, what am I doing wrong? (Across Classes)
I have two classes: The UI and from the other one another thread is started.
The UI is just a Form (form1), ListBox (listbox1) and a Button (button1)
form1.vb
thread2.vb
Everytime when AddEvent is called from the other thread and reaches InvokeRequired
it is false and insertig the String into the Listbox doesn't work either,
no error but the String is not added.
I have two classes: The UI and from the other one another thread is started.
The UI is just a Form (form1), ListBox (listbox1) and a Button (button1)
form1.vb
Visual Basic:
Public Class Form1
Delegate Sub MsgDlg(ByVal Msg As String)
Dim T As New Thread2
Public Sub AddEvent(ByVal Msg As String)
If ListBox1.InvokeRequired Then
Dim MsgSub As New MsgDlg(AddressOf AddEvent)
ListBox1.Invoke(MsgSub, Msg)
Else
ListBox1.Items.Insert(0, DateTime.Now.ToLocalTime.ToString & ": " & Msg)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
T.Journey()
End Sub
End Class
Visual Basic:
Public Class Thread2
Dim Locations() As String = {"a", "b", "c"}
Public Sub Journey()
Dim Elsewhere As New Threading.Thread(AddressOf Wander)
Elsewhere.Start()
End Sub
Private Sub Wander()
Dim Location As String
For Each Location In Locations
Form1.AddEvent("Reached " & Location)
Next
End Sub
End Class
Everytime when AddEvent is called from the other thread and reaches InvokeRequired
it is false and insertig the String into the Listbox doesn't work either,
no error but the String is not added.
Last edited: