后台工作者,传递变量不起作用,在我的例子中我解释了一切,我只放了重要的函数,我有其他函数来自BackgroundWorker。
int TheFunction(unordered_map<std::string,std::string> options, BackgroundWorker^ worker, DoWorkEventArgs ^ e){
if(options["option1"].compare("options") == 0){
//...
}
return 0;
}
void backgroundWorker2_DoWork(Object^ sender, DoWorkEventArgs^ e ){
BackgroundWorker^ worker = dynamic_cast<BackgroundWorker^>(sender);
//e->Result = TheFunction( safe_cast<Int32>(e->Argument), worker, e ); //That's how I do to send an integer value and works just fine, but I don't know how to send non-numeric values with safe_cast or something that works, in the function it looks like this: TheFunction(int index, ...) it works fine, I want to know with unordered_map or with strings also would work, I want more than one argument if you can do with std::string
e->Result = TheFunction(safe_cast<unordered_map<std::string,std::string>>(e->Argument)); //I tried this, and it didn't work
}
void CallBackgroundWorker(){
this->backgroundWorker2 = gcnew System::ComponentModel::BackgroundWorker;
this->backgroundWorker2->WorkerReportsProgress = true;
this->backgroundWorker2->WorkerSupportsCancellation = true;
this->backgroundWorker2->DoWork += gcnew DoWorkEventHandler( this, &GUISystem::backgroundWorker2_DoWork );
this->backgroundWorker2->RunWorkerCompleted += gcnew RunWorkerCompletedEventHandler( this, &GUISystem::backgroundWorker2_RunWorkerCompleted );
this->backgroundWorker2->ProgressChanged += gcnew ProgressChangedEventHandler( this, &GUISystem::backgroundWorker2_ProgressChanged );
unordered_map<std::string,std::string>* options = unordered_map<std::string,std::string>();
options["option1"] = "valor1";
options["option2"] = "valor2";
this->backgroundWorker2->RunWorkerAsync(options);
}
那么我该如何发送unordered_map或std :: string(超过1个参数)?
提前谢谢。这会有很大的帮助。答案 0 :(得分:0)
这一行:
unordered_map<std::string,std::string>* options = unordered_map<std::string,std::string>();
即使在标准C ++中,也不合法。指针需要存储地址,而不是对象。所以你可能想说new
使用动态分配(毕竟,对象需要存活直到回调在另一个线程上运行)。
此时,您可以将指针包裹在System::IntPtr
内,并在回调中投射ToPointer()
的结果。