procedure XXXX; var Mythreadtask : ITask; begin if Assigned(Mythreadtask) then begin if Mythreadtask.Status = TTaskStatus.Running then begin //If it is already running don't start it again Exit; end; end; Mythreadtask := TTask.Create (procedure () var I:integer; begin ////////// 需要执行的代码 ////////// end); Mythreadtask.Start; end;
当一个form中需要多次使用线程时,发现: 比如:在线程里执行完下述: ClientModule1.ClientDataSet1.Close; ClientModule1.ClientDataSet1.CommandText:='select XXX from XXX'; ClientModule1.ClientDataSet1.Open; 后,在其他的功能里,执行查询:(没放到线程里) ClientModule1.ClientDataSet1.Close; ClientModule1.ClientDataSet1.CommandText:='select a1,a2,a3 from XXX'; ClientModule1.ClientDataSet1.Open; if ClientModule1.clientDataSet1.recordcount >0 then begin //这里可以查询到recordcount>0 但是: 变量:= ClientModule1.ClientDataSet1.FieldByName('a1').AsString;
1) any "ERROR" = Access Violation, Exception, etc... into a "thread" do stop it!!!!
then, you need always use "TRY... EXCEPT... END"
2) dont use "share any db component in thread and main-thread"...
3) if needs updates de UI, use: a) xx.Synchronize(...) = for try update "imediatelly" b) xx.Queue(...) = for try update "when possible"
try this way:
procedure ThreadXXXXX.WWWWWW var myQry:TxxxQuery; // + vars begin myQry:TxxxQuery.create....; try try /// + code qry.Open.... /// + code qry.Close; except // ? ? ? ? end; finally myQry.Free; end; end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3