孟
孟雯
Unregistered / Unconfirmed
GUEST, unregistred user!
我在主程序中用线程执行数据库的查询操作,以便时间长的查询可由用户中断。
所用 Query 拥有自己的 Session、Database 、DataSource和DbGrid;
不同的是:每次执行的查询语句。
另外由于主程序的不同地方调用线程,所以在thread的exeute 事件加了不同的
synchronize(method)代码,来访问VCL资源。我想这个应该不成问题。
我的问题是:
每次查询中断以后,我再次执行新的查询,执行TDBQueryThread.Execute事件会报错
“can't perform this operation on an open dataset”
代码:
TDBQueryThread = class(TThread)
private
{ Private declarations }
FQry_TmT : TQuery;
FDS_TmT : TDataSource;
FDB_TmT : TDataBase;
FSQl_TmT : string;
FRecNum : integer;
F_Flag : Integer;
FQueryException : Exception;
procedure HooKUpUI;
procedure QueryError;
procedure AddSGTM;
Function FindInSG(rn:string;ic:string):integer;
procedure GiveRecNum;
procedure PDoAuto;
procedure PDoPartAuto;
protected
procedure Execute;
override;
public
constructor Create(Qry_TmT: TQuery;
Ds_TmT: TDataSource;Db_TmT:TDataBase;SQL_TmT:String;Flag :Integer);virtual;
end;
var
Main_Form: TMain_Form;
MyThread : TDBQueryThread;
constructor TDBQueryThread.Create(Qry_TmT: TQuery;
Ds_TmT: TDataSource;Db_TmT:TDataBase;SQL_TmT:String;Flag :Integer);
begin
inherited Create(True);
FQry_TmT := Qry_TmT;
FDS_TmT := Ds_TmT;
FDB_TmT := Db_TmT;
FSQl_TmT := Sql_TmT;
F_Flag := Flag;
FreeOnTerminate := True;
Resume;
end;
procedure TDBQueryThread.Execute;
begin
try
with FQry_TmTdo
begin
close;
sql.Clear;*** 中断后执行到此处报错“can't perform this operation on an open dataset”
sql.Add(FSql_TmT);
prepare;
open;
first;
while not eofdo
begin
Inc(FRecNum);
Next;
end;
First;
synchronize(GiveRecNum);
Case F_Flag of
0: begin
synchronize(AddSGTM);
synchronize(PDoAuto);
end;
1: begin
synchronize(HookUpUI);
synchronize(PDoPartAuto);
end;
...
end;
end;
except
FQueryException:=ExceptObject as Exception;
Synchronize(QueryError);
end;
end;
调用: MyThread := TDBQueryThread.Create(Dm_Form.QryTm,Dm_Form.DS_Tm,Dm_Form.DBS_Qry,S_Sql,0);
挂起: MyThread.Suspend ;
MyThread.Free;
各位大侠可看出问题来了吗???
期盼援手。。
100分奉上,不够再加。
多谢!
所用 Query 拥有自己的 Session、Database 、DataSource和DbGrid;
不同的是:每次执行的查询语句。
另外由于主程序的不同地方调用线程,所以在thread的exeute 事件加了不同的
synchronize(method)代码,来访问VCL资源。我想这个应该不成问题。
我的问题是:
每次查询中断以后,我再次执行新的查询,执行TDBQueryThread.Execute事件会报错
“can't perform this operation on an open dataset”
代码:
TDBQueryThread = class(TThread)
private
{ Private declarations }
FQry_TmT : TQuery;
FDS_TmT : TDataSource;
FDB_TmT : TDataBase;
FSQl_TmT : string;
FRecNum : integer;
F_Flag : Integer;
FQueryException : Exception;
procedure HooKUpUI;
procedure QueryError;
procedure AddSGTM;
Function FindInSG(rn:string;ic:string):integer;
procedure GiveRecNum;
procedure PDoAuto;
procedure PDoPartAuto;
protected
procedure Execute;
override;
public
constructor Create(Qry_TmT: TQuery;
Ds_TmT: TDataSource;Db_TmT:TDataBase;SQL_TmT:String;Flag :Integer);virtual;
end;
var
Main_Form: TMain_Form;
MyThread : TDBQueryThread;
constructor TDBQueryThread.Create(Qry_TmT: TQuery;
Ds_TmT: TDataSource;Db_TmT:TDataBase;SQL_TmT:String;Flag :Integer);
begin
inherited Create(True);
FQry_TmT := Qry_TmT;
FDS_TmT := Ds_TmT;
FDB_TmT := Db_TmT;
FSQl_TmT := Sql_TmT;
F_Flag := Flag;
FreeOnTerminate := True;
Resume;
end;
procedure TDBQueryThread.Execute;
begin
try
with FQry_TmTdo
begin
close;
sql.Clear;*** 中断后执行到此处报错“can't perform this operation on an open dataset”
sql.Add(FSql_TmT);
prepare;
open;
first;
while not eofdo
begin
Inc(FRecNum);
Next;
end;
First;
synchronize(GiveRecNum);
Case F_Flag of
0: begin
synchronize(AddSGTM);
synchronize(PDoAuto);
end;
1: begin
synchronize(HookUpUI);
synchronize(PDoPartAuto);
end;
...
end;
end;
except
FQueryException:=ExceptObject as Exception;
Synchronize(QueryError);
end;
end;
调用: MyThread := TDBQueryThread.Create(Dm_Form.QryTm,Dm_Form.DS_Tm,Dm_Form.DBS_Qry,S_Sql,0);
挂起: MyThread.Suspend ;
MyThread.Free;
各位大侠可看出问题来了吗???
期盼援手。。
100分奉上,不够再加。
多谢!