C# 参数计数不匹配。-穿线
我正在尝试使用WPF创建一个多客户端/服务器聊天小应用程序,但我遇到了一些问题。不幸的是,当我按下“连接”按钮时,程序崩溃了 到目前为止,我已经通过客户端程序(线程)实现了这一点:C# 参数计数不匹配。-穿线,c#,wpf,multithreading,C#,Wpf,Multithreading,我正在尝试使用WPF创建一个多客户端/服务器聊天小应用程序,但我遇到了一些问题。不幸的是,当我按下“连接”按钮时,程序崩溃了 到目前为止,我已经通过客户端程序(线程)实现了这一点: public委托void UpdateText(object txt) 我得到了这个方法: private void UpdateTextBox(object txt) { if (msg_log.Dispatcher.CheckAccess()) {
public委托void UpdateText(object txt)代码>
我得到了这个方法:
private void UpdateTextBox(object txt)
{
if (msg_log.Dispatcher.CheckAccess())
{
Dispatcher.Invoke(new UpdateText(UpdateTextBox),txt);
}
else
{
msg_log.Dispatcher.Invoke(new UpdateText(UpdateTextBox), txt);
}
}
我使用按钮点击事件连接到服务器,如下所示:
private void connect_Click(object sender, RoutedEventArgs e)
{
if ((nick_name.Text == "") || (ip_addr.Text == "") || (port.Text == ""))
{
MessageBox.Show("Nick name, IP Address and Port fields cannot be null.");
}
else
{
client = new Client();
client.ClientName = nick_name.Text;
client.ServerIp = ip_addr.Text;
client.ServerPort = port.Text;
Thread changer = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(UpdateTextBox));
changer.Start();
client.OnClientConnected += new OnClientConnectedDelegate(client_OnClientConnected);
client.OnClientConnecting += new OnClientConnectingDelegate(client_OnClientConnecting);
client.OnClientDisconnected += new OnClientDisconnectedDelegate(client_OnClientDisconnected);
client.OnClientError += new OnClientErrorDelegate(client_OnClientError);
client.OnClientFileSending += new OnClientFileSendingDelegate(client_OnClientFileSending);
client.OnDataReceived += new OnClientReceivedDelegate(client_OnDataReceived);
client.Connect();
}
}
请注意,OnClient*事件类似于private void client_OnDataReceived(对象发送方,ClientReceivedArguments R){UpdateTextBox(R.ReceivedData);}
因此,这些事件应该在msg_log文本框中写入一些类似“Connected”的文本
顺便说一句,txt对象过去是一个字符串变量,但我改变了它,因为我知道ParameterizedThreadStart
只接受对象作为参数
任何帮助都将不胜感激
提前感谢,,
乔治
编辑:按照Abe Heidebrecht的建议编辑UpdateTextBox方法。调用调用时有一些问题
- 您不需要创建对象数组来传递参数李>
- 传递
DispatcherPriority.Normal
是冗余的(Normal
是默认值)李>
- 您没有将任何参数传递给第二个
Invoke
方法(可能就是发生错误的地方)
应该是这样的:
private void UpdateTextBox(object txt)
{
if (msg_log.Dispatcher.CheckAccess())
{
Dispatcher.Invoke(new UpdateText(UpdateTextBox), txt);
}
else
{
msg_log.Dispatcher.Invoke(new UpdateText(UpdateTextBox), txt);
}
}
编辑StackOverflowException
这将导致StackOverflowException
,因为您正在无限循环中调用您的方法。这是因为您的方法只是一次又一次地调用自己
Dispatcher.Invoke
所做的是在拥有Dispatcher
的线程上调用传递的委托。正因为msg_log
可能有不同的调度程序,当您调用UpdateTextBox
时,您正在向当前方法传递一个委托,这会导致无限循环
您真正需要做的是在msg_log对象上调用一个方法,如下所示:
private void UpdateTextBox(object txt)
{
if (msg_log.Dispatcher.CheckAccess())
{
if (txt != null)
msg_log.Text = txt.ToString();
}
else
{
msg_log.Dispatcher.Invoke(new UpdateText(UpdateTextBox), txt);
}
}
首先感谢您的快速回复。好吧,现在这个错误似乎已经解决了,但是当我按下连接按钮时,另一个错误消息出现了。WindowsBase.dll中发生“System.StackOverflowException”类型的未处理异常。请注意,OnClient*事件类似于“private void client_OnDataReceived(object Sender,ClientReceiveArguments R){UpdateTextBox(R.ReceivedData);}”,因此这些事件应该写入一些类似于“Connected”的文本进入msg_日志TextBox@GeorgeChondrompilas堆栈溢出表明正在进行递归循环。也就是说,您调用的方法所做的任何更改都会导致UpdateTextBox
再次触发,导致更多的更改,导致更多的触发器,等等。@georgechondompilas我已更新了答案以解决此问题。@abehidebrecht谢谢,但这似乎不起作用。System.Windows.Controls.TextBox不包含UpdateTextBox方法。@GeorgeSchondompilas好的,我以为您的msg_日志是您实现的自定义类。我已经更新了答案以设置msg_log.Text属性。