How do I pass a struct to a backgroundworker thread in C++/CLI
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
//...
}
Looks like you need to declare your struct as a managed struct by using the keyword ref
ref struct ArgumentList {
int x;
string text1;
};
That way it will correctly be inherited from the Object type ( the base object for all managd classes ) that RunWorkerAsync(Object) expects
精彩评论