这样行不?请高手指正!怎样才能用线程实现ProgressBar和Query1的同步?不用事件,对事件我自己也能做
TSaveThread=class(TThread)
private
FProgressBar:TProgressBar;
FQuery:TQuery;
FNowExecSQL: Boolean;
procedure BarFwAfterExecSQL;
protected
procedure Execute;override;
public
constructor Create(Suspended:Boolean;ProgressBar:TProgressBar;
Query:TQuery);
property NowExecSQL:Boolean read FNowExecSQL write FNowExecSQL;
end;
{$R *.DFM}
{ TSaveThread }
constructor TSaveThread.Create(Suspended: Boolean;
ProgressBar: TProgressBar;Query:TQuery);
begin
inherited Create(Suspended);
FProgressBar:=ProgressBar;
FQuery:=Query;
end;
procedure TSaveThread.BarFwAfterExecSQL;
begin
//FQuery.ExecSQL;
if FNowExecSQL then
begin
FProgressBar.StepIt;
FNowExecSQL:=False;
end;
end;
procedure TSaveThread.Execute;
begin
while not terminateddo
Synchronize(BarFwAfterExecSQL);
end;
.
.
.
//创建线程
procedure TForm1.FormCreate(Sender: TObject);
begin
FSaveThread:=TSaveThread.Create(False,ProgressBar1,Query1);
end;
//执行调用
procedure TForm1.InsertTeams(TeamsAr:TeamsArray);
var
i:Integer;
SQLText:String;
begin
SQLText:='Insert Into SaveTest Values
PNo,
Team,
Plays,
Points)';
for i:=Low(TeamsAr) to High(TeamsAr)do
begin
with Query1do
begin
Close;
SQL.Clear;
SQL.Text:=SQLText;
ParamByName('PNo').AsString:= TeamsAr[i,1];
ParamByName('PTeam').AsString:= TeamsAr[i,2];
ParamByName('PPlays').AsString:=TeamsAr[i,3];
ParamByName('PPoints').AsString:=TeamsAr[i,4];
ExecSQL;//执行SQL
FSaveThread.NowExecSQL:=True;//调用线程????????
end;
end;
CurRecords;//刷新
end;