ElektroStudios
ElektroStudios

Reputation: 20484

Delegate and thread issue

I have a thread which has a Procedure with this loop:

Private Sub SendFiles()
  For Each Item As ListViewItem In ListView_Monitor.Items

     ' do something...

  Next Item
End Sub

That causes a cross-thread operation error when trying to read the Items collection of the ListView_Monitor

I've tried to write the correct delegate to avoid that error but I'm missing something and does not work my delegate, so for now I set CheckForIllegalCrossThreadCalls to False.

Someone can show me how would be the correct delegate for the operation I need?

Upvotes: 1

Views: 74

Answers (1)

James
James

Reputation: 82136

Don't mess about with a UI component on a non-UI thread, I would advise using Invoke to force your code to run on the UI thread e.g.

ListView_Monitor.Invoke(Sub()
    For Each Item As ListViewItem In ListView_Monitor.Items
        ' do something...
    Next Item
End Sub)

Upvotes: 4

Related Questions