VS C++ InvokeRequired 问题/委托无效
我是 C++ 新手,正在使用 VS2010。 有人可以检查下面的代码并帮助解决它吗?每次函数 UpdateDataGrid(unsigned char CANPacket[15])
被调用时,以下消息将显示在新窗口中,并且应用程序将关闭。
An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll
Additional information: Object of type 'System.Byte' cannot be converted to type 'System.Byte*'.
我必须在这个项目中使用 unsinged char
数据类型,而不是 String^
。 有什么方法可以纠正我的代码吗?
//Piece of my code
namespace VCCDC {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Threading;
public ref class Form1 : public System::Windows::Forms::Form
{
delegate void UpdateDataGridCallback(unsigned char CanPacket[15]);
private: void UpdateDataGrid(unsigned char CANPacket[15]) {
if (this->dataGridView1->InvokeRequired) {
UpdateDataGridCallback^ d = gcnew UpdateDataGridCallback(this,&VCCDC::Form1::UpdateDataGrid);
this->Invoke(d,gcnew unsigned char(CANPacket[15]));
}
else {
//Update dataGridView1 with new data
}
}
}
}]
I am new to C++ and Im using VS2010.
Could someone check the code below and help to solve it? Everytime the function UpdateDataGrid(unsigned char CANPacket[15])
is called the following message is shown in a new window and the application closes.
An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll
Additional information: Object of type 'System.Byte' cannot be converted to type 'System.Byte*'.
I have to use unsinged char
data type and not String^
in this project.
Is there any way to correct my code?
//Piece of my code
namespace VCCDC {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Threading;
public ref class Form1 : public System::Windows::Forms::Form
{
delegate void UpdateDataGridCallback(unsigned char CanPacket[15]);
private: void UpdateDataGrid(unsigned char CANPacket[15]) {
if (this->dataGridView1->InvokeRequired) {
UpdateDataGridCallback^ d = gcnew UpdateDataGridCallback(this,&VCCDC::Form1::UpdateDataGrid);
this->Invoke(d,gcnew unsigned char(CANPacket[15]));
}
else {
//Update dataGridView1 with new data
}
}
}
}]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将该行更改
为“
您已经有一个
unsigned char
指针”,将其传递。使用gcnew
,您打算创建另一个,但这是不必要的。也是由此
gcnew
行引起的错误。您必须使用Byte
参数构造Byte*
。你的也是Byte*
。Change the line
to
You already have an
unsigned char
pointer, pass it through. Withgcnew
you intent to create another one, which is unnecessary.Also error caused by this
gcnew
line. You have to construct theByte*
with aByte
parameter. Yours isByte*
too.