使用BeginInvoke时出现参数计数不匹配异常
Parameter count mismatch exception when using BeginInvoke
我在一个运行async的C++.NET表单应用程序中有一个后台工作者。在这个后台工作者的DoWork函数中,我想将行添加到数据网格视图中,但我真的不知道如何使用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 );
}
我得到的错误是:
类型为的未处理异常中出现"System.Reflection.TargetParameterCountException"mscorlib.dll
附加信息:参数计数不匹配。
当我更改为不传递任何参数的代码时,它只是起作用,代码变为:
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
获取参数的数组,然后您将恰好是一个参数的数组传递给它。
参数
方法
类型:
System.Delegate
方法的委托,该方法接受
args
中指定的参数,该参数被推送到Dispatcher事件队列中。args
型号:
System.Object[]
要作为参数传递给给定方法的对象数组。可以是
null
。
因此,BeginInvoke
将其视为方法具有3字符串参数:"test"
、"test2"
和"test3"
。您需要传递一个仅包含row
:的数组
array<Object^>^ parms = gcnew array<Object^> { row };
ovlgrid.BeginInvoke(gcnew invokeDelegate(this, &Form1::AddRow), parms);
相关文章:
- Qt SQLite没有查询或参数计数不匹配
- 模板参数推导失败,函数参数/参数不匹配
- 在使用累加时,C++中的运算符+不匹配
- C++ 与操作员不匹配<<
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 与'operator='不匹配(操作数类型'String'且"void")
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- C4018:类内有符号、无符号不匹配
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 为什么我收到错误:"运算符<<不匹配?
- RE2 不匹配非 ASCII 字符
- 函数签名与调用的函数不匹配,常量字符[]和字符*之间的区别?
- 模板推导:为什么函数指针模板定义在常量和/或引用时不匹配?
- 错误:"模板<类_Tp,类_Dp>类 std::unique_ptr"的模板参数列表中参数 1 的类型/值不匹配
- OpenSSL fips in C++ wrapper Library 如何?错误:指纹不匹配
- 为什么我的数组值与此处的全局变量不匹配?
- 与异常处理程序中的操作员<<不匹配
- 使用BeginInvoke时出现参数计数不匹配异常
- Android OpenCV imread 标志致命异常,imread 在没有标志的情况下工作正常,像素输出与 MATLAB 不匹配
- 提升 ASIO 异常:use_private_key_file:键值不匹配