加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 使用BeginInvoke时的参数计数不匹配异常

发布时间:2020-12-16 07:28:46 所属栏目:百科 来源:网络整理
导读:我在C .NET表单应用程序中有一个后台工作程序,它运行异步.在这个后台工作者的DoWork函数中,我想向datagridview添加行,但是我无法弄清楚如何使用BeginInvoke执行此操作,因为我的代码似乎不起作用. 我的代码 delegate void invokeDelegate(arrayString^^row);.
我在C .NET表单应用程序中有一个后台工作程序,它运行异步.在这个后台工作者的DoWork函数中,我想向datagridview添加行,但是我无法弄清楚如何使用BeginInvoke执行此操作,因为我的代码似乎不起作用.

我的代码

delegate void invokeDelegate(array<String^>^row);

....
In the DoWork of the backgroundworker
....

array<String^>^row = gcnew array<String^>{"Test","Test","Test"};
if(ovlgrid->InvokeRequired)
    ovlgrid->BeginInvoke(gcnew invokeDelegate( this,&Form1::AddRow),row);

....

void AddRow(array<String^>^row)
{
 ovlgrid->Rows->Add( row );
}

我得到的错误是:

An unhandled exception of type
‘System.Reflection.TargetParameterCountException’ occurred in
mscorlib.dll

Additional information: Parameter count mismatch.

当我更改为代码以不传递任何参数它只是工作,代码而不是:

delegate void invokeDelegate();

...
In the DoWork function
...

if(ovlgrid->InvokeRequired)
     ovlgrid->BeginInvoke(gcnew invokeDelegate( this,&Form1::AddRow));

...
void AddRow()
{
     array<String^>^row = gcnew array<String^>{"test","test2","test3"};
     ovlgrid->Rows->Add( row );
}

但问题是我想传递参数.
我想知道我做错了什么导致了parametercountexception以及如何解决这个问题?

解决方法

你遇到的问题是 BeginInvoke takes an array of parameters,你传递一个恰好是一个参数的数组.

Parameters

method

Type: System.Delegate

The delegate to a method that takes parameters specified in args,which is pushed onto the Dispatcher event queue.

args

Type: System.Object[]

An array of objects to pass as arguments to the given method. Can be null.

因此,BeginInvoke认为这意味着您有3个字符串参数:“test”,“test2”和“test3”.您需要传递一个只包含您的行的数组:

array<Object^>^ parms = gcnew array<Object^> { row };
ovlgrid.BeginInvoke(gcnew invokeDelegate(this,parms);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读