unordered_map<std::string,unordered_map<std::string, std::string> >* storing_vars;
我在作用域中声明的作用域中有这个变量。
这是在构造函数中声明的。
this->storing_vars = new unordered_map<std::string,unordered_map<std::string, std::string> >();
为了初始化它。
然后我所做的是我的 BackgroundWorker 一遍又一遍地调用一个函数
for(int i2 = 0; i2 < 30; i2++){
int index_pos_curr = i2;
//Start the Threads HERE
this->backgroundWorker2 = gcnew System::ComponentModel::BackgroundWorker;
this->backgroundWorker2->WorkerReportsProgress = true;
this->backgroundWorker2->WorkerSupportsCancellation = true;
//this->backgroundWorker2->FieldSetter(L"std::string",L"test","damnnit");
backgroundWorker2->DoWork += gcnew DoWorkEventHandler( this, &MainFacebook::backgroundWorker2_DoWork );
backgroundWorker2->RunWorkerCompleted += gcnew RunWorkerCompletedEventHandler( this, &MainFacebook::backgroundWorker2_RunWorkerCompleted );
backgroundWorker2->ProgressChanged += gcnew ProgressChangedEventHandler( this, &MainFacebook::backgroundWorker2_ProgressChanged );
backgroundWorker2->RunWorkerAsync(index_pos_curr);
Sleep(50); //THE PROBLEM IS HERE, IF I COMMENT THIS OUT it won't work, that's probably because there are a lot of functions trying to add values in the same variable (even though the indexes are differents in each call)
}
完成后,它会调用 DoWork 函数
void backgroundWorker2_DoWork(Object^ sender, DoWorkEventArgs^ e ){
BackgroundWorker^ worker = dynamic_cast<BackgroundWorker^>(sender);
e->Result = SendThem( safe_cast<Int32>(e->Argument), worker, e );
}
int SendThem(int index){
stringstream st;
st << index;
//...
(*this->storing_vars)[st.str()]["index"] = "testing1";
(*this->storing_vars)[st.str()]["rs"] = "testing2";
return 0;
}
当我在该行中添加注释时Sleep(50)
,我认为问题在于,由于后台线程调用相同的函数,因此在多次调用时存储数据可能会出现问题,甚至可能没有等待其他存储到完成后,它会导致“xhash.h”文件中出现错误,该错误已通过 using 进行清理Sleep(50)
,但我无法使用这些错误,因为它会冻结我的 UI,而且 50 毫秒是我假设它已经存储的时间变量值,但是如果在较慢的计算机上需要更长的时间怎么办?这不是正确的方法。
我该怎么做才能解决这个问题?
我希望能够在不使用 SLEEP 的情况下更新 unordered_map
提前致谢。