gpt4 book ai didi

c# - 客户端服务器套接字应用程序上的方法错误没有过载

转载 作者:行者123 更新时间:2023-12-03 12:04:39 24 4
gpt4 key购买 nike

我正在创建一个客户端/服务器 WPF 应用程序,如果客户端尚未连接,则服务器应用程序将新的客户端信息添加到 ListView 项,或者如果它们已经连接,则更新该特定客户端的信息 OnDataReceived。我收到“没有重载 -- 匹配委托(delegate) -- 错误”,但我真的不明白为什么。有人可以告诉我我做错了什么吗?

顺便说一句,我对服务器/客户端套接字通信很陌生,所以如果有人能指出我一些资源,我将不胜感激。

(更新为 bkribbs 答案)

// Error is here:
private void UpdateClientListControl()
{
if (Dispatcher.CheckAccess())
{
var lv = listBoxClientList;
listBoxClientList.Dispatcher.BeginInvoke(new UpdateClientListCallback(UpdateClientList), new object[] { this.listBoxClientList, false, null });

//No overload for 'UpdateClientList' matches delegate 'UpdateClientListCallback'
//I think the error is in how i added these additional parameters, but I tried using 'bool AlreadyConnected' and 'ClientInfo CurrentClient' and
//I get more errors 'Only assignment, call, incriment, ... can be used as a statement'

}
else
{
UpdateClientList(this.listBoxClientList);
}
}


// This worked fine until I added bool Alreadyconnected and CurrentClient
void UpdateClientList(ListView lv, bool AlreadyConnected=false, ClientInfo CurrentClient = null)
{
if (AlreadyConnected)
{
//Updates listview with CurrentClient information that has changed
}
else
{
//Updates listview with new client information
}
}

我如何尝试在 OnDataReceived 中使用它:
public void OnDataReceived(IAsyncResult asyn)
{
//after receiving data and parsing message:
if(recieved data indicates already connected)
{
UpdateClientList(this.listBoxClientList, true, clientInfo);
}
else
{
UpdateClientList(this.listBoxClientList);
}

}

最佳答案

你很近。有两个问题。

您现在提到的一个是因为您添加了两个额外的参数,因此您尚未更新 UpdateClientListCallback 的委托(delegate)声明。

现在它看起来像:

delegate void UpdateClientListCallback(ListView lvi);

您需要将其更改为:
delegate void UpdateClientListCallback(ListView lvi, bool AlreadyConnected, ClientInfo CurrentClient);

您会很快发现的另一个问题是您的参数有点错误。您正在使用 Dispatcher.BeginInvoke(Deletegate, Object[])
因此,要解决您的问题,请替换:
listBoxClientList.Dispatcher.BeginInvoke(new UpdateClientListCallback(UpdateClientList), this.listBoxClientList, false, null);

和:
object[] parameters = new object[] { this.listBoxClientList, false, null };
listBoxClientList.Dispatcher.BeginInvoke(new UpdateClientListCallback(UpdateClientList), parameters);

或一个不错的类轮:
listBoxClientList.Dispatcher.BeginInvoke(new UpdateClientListCallback(UpdateClientList), new object[] { this.listBoxClientList, false, null });

关于c# - 客户端服务器套接字应用程序上的方法错误没有过载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33509250/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com