[RESOLVED] [2005] Add items through a different thread
I am trying to use a filewatcher to write changes to a listbox but can't figure it out. I got an MSDN example working but it is a richtextbox. How to use invoke with adding items to a listbox(names of files in this case)?
This is the textbox example
VB Code:
' This delegate enables asynchronous calls for setting
' the text property on a TextBox control.
Delegate Sub SetTextCallback(ByVal [text] As String)
Private Sub Cookie(ByVal Source As Object, ByVal e As System.IO.FileSystemEventArgs)
' This is the subroutine that responds to either the Changed or Created events
SetText(e.Name)
'results in a CrossThreadCall ex/warning:
'lbChanges.Items.Add(e.name)
Beep()
End Sub
' If the calling thread is the same as the thread that created
' the TextBox control, the Text property is set directly.
Private Sub SetText(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 Me.lbChanges.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetText)
Me.Invoke(d, New Object() {[text]})
Else
Me.rtb1.Text = [text]
End If
End Sub
Re: [2005] Add items through a different thread
I got it working but also found a better way.