在主线程 MdlMain函数中测试读取ma文件中的资源没有问题。
但在新开线程关联的outputMsg函数无法读取资源。
不知用何办法解决?谢谢老师。
void outputMsg(){ WString outMsg; mdlResource_loadWString(outMsg, 0, STRINGLISTID_Prompts, 1); mdlResource_loadWString(outMsg, 0, STRINGLISTID_Prompts, 2);}
extern "C" void MdlMain(int argc, WCharCP argv[]){
RscFileHandle rfHandle; mdlResource_openFile(&rfHandle, NULL, RSC_READ); mdlSystem_registerCommandNumbers(s_commandNumbers); if (mdlParse_loadCommandTable(NULL) == NULL) mdlOutput_rscPrintf(MSG_ERROR, NULL, STRINGLISTID_Messages, MSGID_CommandTable);
mdlState_registerStringIds(STRINGLISTID_Commands, STRINGLISTID_Prompts);
//下面测试没有问题,能读取资源
WString outMsg; mdlResource_loadWString(outMsg, 0, STRINGLISTID_Prompts, 1); mdlDialog_dmsgsPrint(outMsg.GetWCharCP()); mdlResource_loadWString(outMsg, 0, STRINGLISTID_Prompts, 2); mdlDialog_dmsgsPrint(outMsg.GetWCharCP());
//
//下面新开线程关联的outputMsg函数无法读取资源
std::thread t(outputMsg);
if (t.joinable()) t.join();
}
测试一下在新的线程中用mdlResource_openFile(&rfHandle, NULL, RSC_READ);再次以只读方式打开ma文件,然后将返回的rfHandle传入你的mdlResource_loadWString的第二个参数。
不过在Bentley公司内部都是不推荐使用多线程编程的。只是在个别荷载很重的地方(如Luxology Rendering)才会启用多线程。
Answer Verified By: 管华明
谢谢,我试试。