多线程,动态创建的控件竟然必须放到Synchronize里面执行,为什么呢。没道理啊!
void __fastcall TChaXunThread::Execute(){ retnum = 6; try { char cuser[MAX_PATH] = ""; char cpass[MAX_PATH] = ""; strcpy(cuser, user.c_str()); strcpy(cpass, pass.c_str()); if(filepath.SubString(1, 2) == "\\\\") { DWORD retcode = NetConnect2(ExtractFileDir(filepath).c_str(), cuser, cpass); if(retcode == 0) { if(FileExists(filepath)) { retnum = 1; if(ExtractFileExt(filepath).LowerCase() == ".dbf" && !sqltxt.Trim().IsEmpty()) { String filename = ChangeFileExt(ExtractFileName(filepath), ""); TDbf *DbfThread; DbfThread = new TDbf(NULL); DbfThread->Active = false; DbfThread->TableName = filepath; DbfThread->ReadOnly=true; DbfThread->Active = true; TxQuery *xQueryThread; xQueryThread = new TxQuery(NULL); xQueryThread->DataSets->Clear(); xQueryThread->AddDataSet(DbfThread, filename); xQueryThread->SQL->Clear(); xQueryThread->SQL->Text = sqltxt; xQueryThread->Open(); //这句Open的语句,必须用Synchronize放到主线程执行否则报错。为什么呢。我这里明明是动态创建的。 retStr = "有" + IntToStr(xQueryThread->RecordCount) + "条,点击查看明细"; //delete DbfThread, xQueryThread; } } else { retnum = 2; } } if(retcode == 1326) { retnum = 4; } if(retcode == 1219) { retnum = 7; } } else { if(FileExists(filepath)) { retnum = 1; } else { retnum = 2; } } } catch(...) { retnum = 5; // throw; } Synchronize(UpdateChaXunGrid);}