调用问题(委托) [英] Problem with Invoking (Delegate)

查看:89
本文介绍了调用问题(委托)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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是ListView.我想要的是,当客户端连接到服务器(上面的代码)时,它将在列表视图中列出.在此先谢谢您.


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.

推荐答案

Adadero,好久不见了;)

正如您似乎在混合使用VB和C#(顺便说一句对您有好处),我将在C#中发布您的其他问题中的相关问题.毫无疑问,您可以自己修改为VB等效项.

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.

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



现在看来,您显然需要用ListView替换txtChat.

如你所知;如果您还有其他问题;只是问问.

干杯,AT



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


这篇关于调用问题(委托)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆