[RESOLVED] Return listview item collection in multithread?
I am doing this within a thread:
Code:
For Each item As ListViewItem In lvUsers.Items
and I am getting: Cross-thread operation not valid: Control 'lvUsers' accessed from a thread other than the thread it was created on.
I have been able to adjust a control in a way using something like:
Code:
Public Delegate Sub ListAddNameInvoker(ByVal text As String)
Public Sub ListAddName(ByVal text As String)
If lvUsers.InvokeRequired Then
lvUsers.Invoke(New ListAddNameInvoker(AddressOf ListAddName), text)
Else
lvUsers.Items.Add(text)
End If
End Sub
But how do I get data from a control and pass it to another thread?
Re: Return listview item collection in multithread?
Check out this thread, might help you out :D.
Re: Return listview item collection in multithread?
You can't get data from a control in other than the UI thread. As such, you will need to get all the data you require out of the ListView first, into an array or collection, and then pass that data to the method that you execute on the secondary thread. For one way of doing that check out this. You might also use a BackgroundWorker and pass an argument to the RunWorkerCompleted method, or you might also use the ThreadPool and pass a second argument to the QueueUserWorkItem method.