【问题标题】:system.reflection.targetparametercountexception parameter count mismatch Parameter count mismatchsystem.reflection.targetparametercountexception 参数计数不匹配 参数计数不匹配
【发布时间】:2013-10-17 11:57:43
【问题描述】:

我正在尝试使用不同的线程更新 UI 并使用以下过程来执行此操作。但在调用期间出现上述错误。请告知这是不允许的。

    delegate void SetLabelCallback(string text,string Qmgr);
    private void Set_status(string text, string Qmgr)
    {
        if (this.Status1A.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(record_count);
            this.Invoke(d, new object[] { text,Qmgr });
        }
        else
        {
            switch (Qmgr)
            {
                case "GCSSPR1A": this.Status1A.Text = text;
                    break;
                case "GCSSPR1B": this.B1_Status.Text = text;
                    break;
                case "GCSSPR2A": this.A2_Status.Text = text;
                    break;
                case "GCSSPR2B": this.B2_Status.Text = text;
                    break;
                case "GCSSPR3A": this.A3_Status.Text = text;
                    break;
                case "GCSSPR3B": this.B3_Status.Text = text;
                    break;
            }

        }

【问题讨论】:

  • record_count的定义是什么?
  • 您使用的是 C# 4 吗?如果是这样,你可以让这段代码更简单,更不容易出错。
  • 我的代码中的错字已修复...
  • 只是出于兴趣,是否有更长的异常消息allin 小写? :)

标签: c# multithreading winforms invoke


【解决方案1】:

我也会像 Baldrick 那样做。

他正在使用 lambda 表达式,也许你会使用类似的东西

private void Set_status(string text, string Qmgr)
{ 
    if (this.InvokeRequired)
    {
    this.Invoke(new ReceivedEventHandler(Set_status), new Object[] {text, Qmgr});                
    }
    else 
    {
    }
}

但是,我认为这不是问题。

我之前收到过这个问题,因为委托处理程序/函数调用中的参数数量与 Invoke 声明中定义的对象数量不匹配。

this.Invoke(d, new object[] { text, Qmgr, something_missing });

希望对你有帮助。

【讨论】:

  • “委托处理程序/函数调用中的参数数量与 Invoke 声明中定义的对象数量不匹配。” + 1
【解决方案2】:

尝试将函数的顶部更改为以下内容:

private void Set_status(string text, string Qmgr)
{
    if (this.Status1A.InvokeRequired)
    {
        this.Invoke((Action)(() => Set_status(text, Qmgr)));
    }
    else
    {

这样你就不需要委托声明等了。

【讨论】:

    猜你喜欢
    • 2016-09-21
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 2017-12-31
    • 2012-06-17
    • 1970-01-01
    相关资源
    最近更新 更多