thread TThread
class TSleepFunc : public TCppInterfacedObject<TProc> { public: TSleepFunc(TForm28* Form) : FForm(Form) { } virtual void __fastcall Invoke(void) { for (FForm->ClientDataSet1->First(); !FForm->ClientDataSet1->Eof; FForm->ClientDataSet1->Next()) { FForm->ClientDataSet1->Edit(); FForm->ClientDataSet1->FieldByName("Stat")->Value = FForm->ClientDataSet1->RecNo; FForm->ClientDataSet1->Post(); } TThread::Synchronize(TThread::CurrentThread, UpdateCaption); } void __fastcall UpdateCaption(void) { FForm->Caption = "OK"; } private: TForm28* FForm; }; // --------------------------------------------------------------------------- void __fastcall TForm28::Button1Click(TObject *Sender) { ClientDataSet1->Close(); ADOQuery1->Close(); ADOQuery1->SQL->Text = "select * from tt"; ADOQuery1->Open(); ClientDataSet1->Open(); TThread::CreateAnonymousThread(new TSleepFunc(this))->Start(); }
http://codeverge.com/embarcadero.cppbuilder.cpp/trttimethod-do-not-work/1055179
delphi
procedure TForm1.ConnectionTest; var sc :TSQLConnection; begin sc := TSQLConnection.Create(nil); sc.DriverName := 'DevartOracle'; sc.Params.Values['DATABASE'] := eDatabase.Text; sc.Params.Values['User_Name'] := eUserName.Text; sc.Params.Values['Password'] := ePassword.Text; sc.Connected := True; Sleep(1000); sc.Connected := False; sc.Free; end; procedure TForm1.btnMainThreadClick(Sender: TObject); begin ConnectionTest; //Does not leak handles with Oracle 12.1.0 client. end; procedure TForm1.btnThreadedClick(Sender: TObject); begin TThread.CreateAnonymousThread(ConnectionTest).Start; //Leaks 9 mutex handles with Oracle 12.1.0 client. end;
Queue依然会阻塞。
TThread.Queue(TThread.CurrentThread, procedure begin FOnProcessPlay(newPos); end);
tthread.Synchronize(TThread.CurrentThread,updateUI);
tthread.CreateAnonymousThread(updateUI).Start;
TThread.Queue(TThread.CurrentThread, startData2Background);
Synchronize代码是阻塞执行的。query.Open()放在这个函数执行是达不到线程效果的。