1

我正在尝试创建一个服务器/多客户端聊天程序。客户端程序完美运行。问题出在服务器上。当我单击连接按钮时,客户端应该连接到服务器,但是

An unhandled exception of type 'System.Reflection.TargetParameterCountException' occurred in mscorlib.dll 
Additional information: Parameter count mismatch.

出现,服务器程序崩溃。我很确定会发生这种情况,因为我希望服务器端程序将客户端的 ip 和昵称保存到 2 个 ListBoxes 中。我的活动:

        private void server_OnClientConnected(object Sender, ConnectedArguments R)
    {
        server.BroadCast(R.Name + " has connected."); //That message is shown at client's chat box. 
                                                      //So there is no problem with the connection.

        UpdateListbox(un_list, R.Name, false); //Here is the problem. It works when I comment out them, 
                                               //without updating the list boxes of course
        UpdateListbox(ip_list, R.Ip, false);
    }

当客户端连接时。

        private void server_OnClientDisconnected(object Sender, DisconnectedArguments R)
    {
        server.BroadCast(R.Name + " has disconnected.");

        UpdateListbox(un_list,R.Name,true);
        UpdateListbox(ip_list, R.Ip, true);

    }

当客户端断开连接时。

我的方法:

    public delegate void UpdateList(ListBox box,object value,bool Remove);


    private void UpdateListbox(ListBox box, object value, bool Remove)
    {
        if (box.Dispatcher.CheckAccess())
        {
            if (value != null && Remove==false)
                box.Items.Add(value);
            else if (value != null && Remove==true)
                box.Items.Remove(value);

        }
        else
        {
            box.Dispatcher.Invoke(new UpdateList(UpdateListbox), value);
        }

    }

在此先感谢,乔治

4

1 回答 1

1

您忘记传递bool Remove参数。将您的行更改为:

box.Dispatcher.Invoke(new UpdateList(UpdateListbox), new object[]{box, value, Remove});

或者,如果您想避免将来犯同样的错误,您可以使用带有此重载的 lambda :

box.Dispatcher.Invoke(() => UpdateListBox(box, value, Remove));

如果你忘记了Remove那里的论点,你会收到一个编译时错误。

于 2013-06-28T12:28:02.670 回答