Results 1 to 2 of 2

Thread: [RESOLVED] [2005] Add items through a different thread

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Resolved [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:
    1. ' This delegate enables asynchronous calls for setting
    2.     ' the text property on a TextBox control.
    3.     Delegate Sub SetTextCallback(ByVal [text] As String)
    4.  
    5.  
    6.     Private Sub Cookie(ByVal Source As Object, ByVal e As System.IO.FileSystemEventArgs)
    7.         ' This is the subroutine that responds to either the Changed or Created events     
    8.         SetText(e.Name)
    9.  
    10.         'results in a CrossThreadCall ex/warning:
    11.         'lbChanges.Items.Add(e.name)
    12.         Beep()
    13.  
    14.     End Sub
    15.  
    16.  
    17.     ' If the calling thread is the same as the thread that created
    18.     ' the TextBox control, the Text property is set directly.
    19.     Private Sub SetText(ByVal [text] As String)
    20.  
    21.         ' InvokeRequired required compares the thread ID of the calling thread
    22.         'to the thread ID of the creating thread. If these threads are different, it returns true.
    23.         If Me.lbChanges.InvokeRequired Then
    24.             Dim d As New SetTextCallback(AddressOf SetText)
    25.             Me.Invoke(d, New Object() {[text]})
    26.         Else
    27.             Me.rtb1.Text = [text]
    28.         End If
    29.     End Sub
    VB 2005, Win Xp Pro sp2

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [2005] Add items through a different thread

    I got it working but also found a better way.
    VB 2005, Win Xp Pro sp2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width