J
jason_fox
Unregistered / Unconfirmed
GUEST, unregistred user!
我声明了一个recorder类型TTaskrec, 在Thread create时传递一个TTaskrec变量给FTaskrec
但是在Thread 的Execute调用过程RepairThreadExecute会丢失?!
原码如下。
{在一公共文件中。声明的TTaskrec类型}
type
TTaskRec = record
rId:integer;
rStatus:integer;
rDatabaseName:string[100];
rDatabaseType:integer;
rServerName:string[50];
rUserName:string[50];
rPassword:string[50];
rBackupPath:string[200];
rBackupFileName:string[50];
risCompress:boolean;
risMultiSaved:boolean;
rMethodType:integer;
end;
unit UntQueryThread;
interface
uses
Classes,DB,DBTables,untglobal,DataModMain;
type
TTaskThread = class(TThread)
private
FTaskrec : TTaskRec;
//!
Fstrings1: Tstrings;
Fstrings2: Tstrings;
protected
procedure Execute;
override;
Procedure RepairThreadExecute;
proceduredo
Execute;
//virtual;
abstract;
public
constructor Create(ATaskrec:TTaskrec);
end;
implementation
{ TTaskThread }
constructor TTaskThread.Create(ATaskrec:TTaskrec);
begin
inc(gTaskRunning);
FTaskrec := ATaskrec;
//传递变量,此时变量正常。
inherited Create(False);
//
FreeOnTerminate:=true;
end;
procedure TTaskThread.Execute;
begin
{ Place thread code here }
RepairThreadExecute;
do
Execute;
// record the connect message to table (tasklog)
end;
procedure TTaskThread.doExecute;
begin
{ Place thread code here }
case TDatabaseType(FTaskrec.rDatabaseType) of
dbSybase:
with MainDataModdo
begin
{set the property of database}
DatabaseThd1.DriverName := 'SYBASE';
DatabaseThd1.Params.Clear;
DatabaseThd1.Params.Assign(Fstrings1);
QueryThd1.SQL.clear;
QueryThd1.SQL.Assign(Fstrings2);
{ execute the query.}
//try
DatabaseThd1.Connected := True;
// record the connect message to table (tasklog)
QueryThd1.ExecSQL;
// record the connect message to table (tasklog)
DatabaseThd1.Connected := False;
{set the status of the task. }
updateTaskStatus(FTaskrec.rId,isWaiting);
//except
//end;
end;
// record the connect message to table (tasklog)
dbAnywhere:;
end;
end;
procedure TTaskThread.RepairThreadExecute();
var
lstr1:string;
begin
{***!FTaskrec的值丢失,变为乱七八糟。 这一步有时是好的,有时会丢失。}
case TDatabaseType(FTaskrec.rDatabaseType) of
{***!FTaskrec的值丢失,变为乱七八糟。 到这变量肯定丢了!}
dbSybase:
begin
Fstrings1.Clear;
lstr1:='SERVER NAME='+FTaskrec.rServerName;
Fstrings1.Add(lstr1);
lstr1:='USER NAME='+FTaskrec.rUserName;
Fstrings1.Add(lstr1);
lstr1:='PASSWORD='+FTaskrec.rPassword;
Fstrings1.Add(lstr1);
{lstr1:='DATABASE NAME='+FTaskrec.rDatabaseName;
Fstrings1.Add(lstr1);}
Fstrings2.Clear;
lstr1:='dump database '+FTaskrec.rDatabaseName+' to '''+
FTaskrec.rBackupPath + FTaskrec.rBackupFileName +'''';
Fstrings2.Add(lstr1);
end;
dbAnywhere:
begin
end;
end;
end;
end.
但是在Thread 的Execute调用过程RepairThreadExecute会丢失?!
原码如下。
{在一公共文件中。声明的TTaskrec类型}
type
TTaskRec = record
rId:integer;
rStatus:integer;
rDatabaseName:string[100];
rDatabaseType:integer;
rServerName:string[50];
rUserName:string[50];
rPassword:string[50];
rBackupPath:string[200];
rBackupFileName:string[50];
risCompress:boolean;
risMultiSaved:boolean;
rMethodType:integer;
end;
unit UntQueryThread;
interface
uses
Classes,DB,DBTables,untglobal,DataModMain;
type
TTaskThread = class(TThread)
private
FTaskrec : TTaskRec;
//!
Fstrings1: Tstrings;
Fstrings2: Tstrings;
protected
procedure Execute;
override;
Procedure RepairThreadExecute;
proceduredo
Execute;
//virtual;
abstract;
public
constructor Create(ATaskrec:TTaskrec);
end;
implementation
{ TTaskThread }
constructor TTaskThread.Create(ATaskrec:TTaskrec);
begin
inc(gTaskRunning);
FTaskrec := ATaskrec;
//传递变量,此时变量正常。
inherited Create(False);
//
FreeOnTerminate:=true;
end;
procedure TTaskThread.Execute;
begin
{ Place thread code here }
RepairThreadExecute;
do
Execute;
// record the connect message to table (tasklog)
end;
procedure TTaskThread.doExecute;
begin
{ Place thread code here }
case TDatabaseType(FTaskrec.rDatabaseType) of
dbSybase:
with MainDataModdo
begin
{set the property of database}
DatabaseThd1.DriverName := 'SYBASE';
DatabaseThd1.Params.Clear;
DatabaseThd1.Params.Assign(Fstrings1);
QueryThd1.SQL.clear;
QueryThd1.SQL.Assign(Fstrings2);
{ execute the query.}
//try
DatabaseThd1.Connected := True;
// record the connect message to table (tasklog)
QueryThd1.ExecSQL;
// record the connect message to table (tasklog)
DatabaseThd1.Connected := False;
{set the status of the task. }
updateTaskStatus(FTaskrec.rId,isWaiting);
//except
//end;
end;
// record the connect message to table (tasklog)
dbAnywhere:;
end;
end;
procedure TTaskThread.RepairThreadExecute();
var
lstr1:string;
begin
{***!FTaskrec的值丢失,变为乱七八糟。 这一步有时是好的,有时会丢失。}
case TDatabaseType(FTaskrec.rDatabaseType) of
{***!FTaskrec的值丢失,变为乱七八糟。 到这变量肯定丢了!}
dbSybase:
begin
Fstrings1.Clear;
lstr1:='SERVER NAME='+FTaskrec.rServerName;
Fstrings1.Add(lstr1);
lstr1:='USER NAME='+FTaskrec.rUserName;
Fstrings1.Add(lstr1);
lstr1:='PASSWORD='+FTaskrec.rPassword;
Fstrings1.Add(lstr1);
{lstr1:='DATABASE NAME='+FTaskrec.rDatabaseName;
Fstrings1.Add(lstr1);}
Fstrings2.Clear;
lstr1:='dump database '+FTaskrec.rDatabaseName+' to '''+
FTaskrec.rBackupPath + FTaskrec.rBackupFileName +'''';
Fstrings2.Add(lstr1);
end;
dbAnywhere:
begin
end;
end;
end;
end.