如何在 C++/CLI 中将结构传递给后台工作线程
struct ArgumentList {
int x;
string text1;
};
/////////////////////////////////////////
ArgumentList arg1={12,"text123"}
WorkerThread->RunWorkerAsync(arg1);
我想传递 arg1 但编译器显示“错误 C2664: 'void System::ComponentModel::BackgroundWorker::RunWorkerAsync(System::Object ^)' : 无法将参数 1 从 'ArgumentList' 转换为 'System::Object ^' ”
System::Void backgroundWorker2_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {
//Do stuff with e->Argument
ArgumentList passedarg=(ArgumentList)e->argument; //'type cast' : cannot convert from 'System::Object ^' to 'ArgumentList'
int y=passedarg.x
string text2=passedarg.text1
//...
}
struct ArgumentList {
int x;
string text1;
};
/////////////////////////////////////////
ArgumentList arg1={12,"text123"}
WorkerThread->RunWorkerAsync(arg1);
I want to passed arg1 but the compiler says "error C2664: 'void System::ComponentModel::BackgroundWorker::RunWorkerAsync(System::Object ^)' : cannot convert parameter 1 from 'ArgumentList' to 'System::Object ^' "
System::Void backgroundWorker2_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {
//Do stuff with e->Argument
ArgumentList passedarg=(ArgumentList)e->argument; //'type cast' : cannot convert from 'System::Object ^' to 'ArgumentList'
int y=passedarg.x
string text2=passedarg.text1
//...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您需要使用以下方法将您的结构声明为 托管结构关键字 ref
这样它将正确地从 RunWorkerAsync(Object) 期望
Looks like you need to declare your struct as a managed struct by using the keyword ref
That way it will correctly be inherited from the Object type ( the base object for all managd classes ) that RunWorkerAsync(Object) expects