Subject Issue for process
Author heroes3lover
Hello everyone,
I got a problem.
Because some SQL is huge and make program no response for a while, we
have to use thread to retrieve data, below is my
code,TSQLCompleteDataSetPlus is a component we inherited from
TSQLCompleteDataSet(this is component of dbexpressplus),
but when the process running to qryInThread.open, sometimes the whole
program jammed, and looks like die, and cpu usage is 0%, I think
should be something wrong with thread safe issue, but I can not find
out, cauz all stuff I created them in thread.
Is there anybody can figure out the possible issue?
Thanks very much
Roc


TDataThread = class( TThread )
qryInThread : TSQLQuery;
conInThread : TSQLConnection;
Log : TProcessLogger;
isSelect : Boolean;
constructor Create;
destructor Destory;
procedure WriteStartLog;
procedure WriteEndLog;
procedure WriteError;
end;

TSQLCompleteDataSetPlusThread = class( TDataThread )
public
constructor Create( SQLQuery : TSQLCompleteDataSetPlus );
procedure Execute; override;
end;

{ TSQLThread }

constructor TDataThread.Create;
begin
inherited Create(TRUE);
FreeOnTerminate := FALSE;
qryInThread := TSQLQuery.Create(nil);
conInThread := TSQLConnection.Create(nil);
Log := TProcessLogger.Create;
conInThread.SQLHourGlass := False;
isSelect := True;
end;

destructor TDataThread.Destory;
begin
qryInThread.Close;
conInThread.Connected := False;
while( conInThread.ConnectionState <> csStateClosed ) do;
FreeAndNil(qryInThread);
FreeAndNil(conInThread);
FreeAndNil(Log);
end;


procedure TDataThread.WriteEndLog;
begin
Log.EndLog;
end;

procedure TDataThread.WriteStartLog;
var
TheMsg : String;
begin
case qryInThread.SQLConnection.ConnectionState of
csStateClosed:TheMsg := 'close';
csStateOpen:TheMsg := 'open';
csStateConnecting:TheMsg := 'connecting';
csStateExecuting:TheMsg := 'executing';
csStateFetching:TheMsg := 'fetching';
csStateDisconnecting:TheMsg := 'disconnecting';
End;
Log.StartLog(TheMsg);
end;

procedure TDataThread.WriteError;
begin
LogErrorMessage('Thread Error');
end;

constructor TSQLCompleteDataSetPlusThread.Create(SQLQuery:
TSQLCompleteDataSetPlus);
var
Backup : TSQLConnection;
begin
inherited Create;
Backup := SQLQuery.SQLConnectionPlus;
conInThread.DriverName := Backup.DriverName;
//set conInThread parameters
conInThread.LoginPrompt := False;
conInThread.Connected := True;
qryInThread.Close;
qryInThread.SQLConnection := conInThread;
qryInThread.SQL.Assign(SQLQuery.SQL);
qryInThread.Params.AssignValues(SQLQuery.Params);
end;

procedure TSQLCompleteDataSetPlusThread.Execute;
begin
Try
Synchronize(WriteStartLog);
if isSelect then
qryInThread.Open;
else
qryInThread.ExecSQL;
Synchronize(WriteEndLog);
Except
on e:exception do
Synchronize(WriteError);
end;
Terminate;
end;