没
没理头号
Unregistered / Unconfirmed
GUEST, unregistred user!
线程怎么安全退出?
我有一个线程
constructor TReadDSThread.Create;
begin
FreeOnTerminate := true;
//线程自动销毁
inherited Create(false);
//继承构造函数
end;
procedure TReadDSThread.Execute;
var
fQuery: TQuery;
fSQLStr: string;
fDataSet: TDatabase;
fSession: TSession;
begin
//这里一大堆代码是为了线程安全,这样做对吗?有别的什么办法吗?
fSession := TSession.Create(Application);
fSession.SessionName := '1';
fDataSet := TDatabase.Create(application);
fDataSet.SessionName := fSession.SessionName;
fDataSet.LoginPrompt := false;
fDataSet.Params.Add('user name=' + AUser);
fDataSet.Params.Add('password=' + APass);
fDataSet.AliasName := ADataName;
fDataSet.DatabaseName := '2';
fDataSet.Connected := true;
fQuery := TQuery.Create(Application);
fQuery.DatabaseName := fDataSet.DatabaseName;
fQuery.SessionName := fDataSet.SessionName;
try
while (not Terminated) do
begin
....
fQuery.Close;
fQuery.SQL.Clear;
fQuery.SQL.Add(fSQLStr);
fQuery.ExecSQL;//当线程强行Terminated时,这里会出错,这里怎么处理啊?
...
end;
finally
fQuery.Free;
fDataSet.Free;
fSession.Free;
end;
end;
我有一个线程
constructor TReadDSThread.Create;
begin
FreeOnTerminate := true;
//线程自动销毁
inherited Create(false);
//继承构造函数
end;
procedure TReadDSThread.Execute;
var
fQuery: TQuery;
fSQLStr: string;
fDataSet: TDatabase;
fSession: TSession;
begin
//这里一大堆代码是为了线程安全,这样做对吗?有别的什么办法吗?
fSession := TSession.Create(Application);
fSession.SessionName := '1';
fDataSet := TDatabase.Create(application);
fDataSet.SessionName := fSession.SessionName;
fDataSet.LoginPrompt := false;
fDataSet.Params.Add('user name=' + AUser);
fDataSet.Params.Add('password=' + APass);
fDataSet.AliasName := ADataName;
fDataSet.DatabaseName := '2';
fDataSet.Connected := true;
fQuery := TQuery.Create(Application);
fQuery.DatabaseName := fDataSet.DatabaseName;
fQuery.SessionName := fDataSet.SessionName;
try
while (not Terminated) do
begin
....
fQuery.Close;
fQuery.SQL.Clear;
fQuery.SQL.Add(fSQLStr);
fQuery.ExecSQL;//当线程强行Terminated时,这里会出错,这里怎么处理啊?
...
end;
finally
fQuery.Free;
fDataSet.Free;
fSession.Free;
end;
end;