Click here to Skip to main content
15,881,780 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Cross-thread operation not valid: Control 'lsvClients' accessed from a thread other than the thread it was created on.

How to solve the error above? I think I must create other method and invoke it. But unfortunately, I don't know how. Here is the code that generates such error
VB
Private Delegate Sub _AddClient(ByVal client As Socket)
   Private Sub AddClient(ByVal client As Socket)

       If InvokeRequired Then
           Invoke(New _AddClient(AddressOf AddClient), client)
       End If

       Dim lvi As New ListViewItem(client.LocalEndPoint.ToString())
       lvi.Tag = client
       lsvClients.Items.Add(lvi)
   End Sub

lsvClients is ListView. All I want is when a client connected to the server (code above) it will be listed on the listview. Thanks in advance.
Posted

1 solution

Hi Adadero, long time no see ;)

As you seem to be mixing VB and C# (good for you by the way) I'll just post a related issue from your other issue In C#. No doubt you can modify this yourself to the VB equivalent.

C#
if (InvokeRequired)
{
	Invoke(new MethodInvoker(delegate()
	{
		if (txtChat != null)
		      txtChat.Text += text;
     	} ));
}


Now it seems clear that you need to replace the txtChat with the ListView.

As you know; if you have onother question; just ask.

Cheers, AT
 
Share this answer
 
Comments
adadero 10-Dec-11 1:19am    
Hi. It's work perfectly. Again, I have to thank you. Your solutions are very helpful. You come exactly when I need help. Anyway, I am coding with both language (VB and C#) and actually I prefer C# since it's simpler but VB is easier anyhow. I hope you can help me when I have problem :). Here is the code as you suggest, and it works.

Private Delegate Sub _AddClient(ByVal client As Socket)
Private Sub AddClient(ByVal client As Socket)

If InvokeRequired Then
Invoke(New _AddClient(AddressOf AddClient), client)
Invoke(New _AddLV(AddressOf AddLV), client)
End If
End Sub

Private Delegate Sub _AddLV(ByVal client As Socket)
Private Sub AddLV(ByVal client As Socket)
Dim lvi As New ListViewItem(client.LocalEndPoint.ToString())
lvi.Tag = client
lsvClients.Items.Add(lvi)
End Sub

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900