高手进来瞧瞧我写的服务程序,哪里出错了!(不怕分不够) ( 积分: 100 )

  • 主题发起人 主题发起人 boyjason
  • 开始时间 开始时间
B

boyjason

Unregistered / Unconfirmed
GUEST, unregistred user!
写了一个数据导入服务程序,实现的功能是将远程的oracle中的数据导入到本地的sql server数据库中,服务能正常启动,却怎么都不能终止,贴出服务线程的代码,请高手帮我看看是怎么一回事!
unit Unit_Import;
interface
uses
Classes,Sysutils,DB, ADODB,forms,ActiveX,Windows,Dialogs,DateUtils,Registry;
type
TImport = class(TThread)
private
FAdoConn:TAdoconnection;
FAdoStoredProc:TAdoStoredProc;
FQuery:TAdoquery;
ImportTime,SumNetAge:String;
{ Private declarations }
protected
procedure ConnectDb;
//连接数据库过程
procedure DisConnectDb;
//断开数据库过程
procedure Execute;
override;
public
destructor Destroy;override;
end;

var
Importthread: TImport;
DbConStr,MS_SQLSERVER,MS_SQLUsr,MS_SQLPwd:String;
Oracle_SERVER,Oracle_Usr,Oracle_Pwd:String;
implementation
uses FunCrypt;
procedure TImport.ConnectDb;
var
Reg:TRegistry;
begin
//服务启动时,将数据库参数获得
CoInitialize(nil);
Reg:=TRegistry.Create;
with Regdo
begin
try
RootKey := HKEY_LOCAL_MACHINE;
OpenKey('SOFTWARE/我的软件',False);
MS_SQLSERVER:= ReadString('MSSQLSRV');
MS_SQLUsr:=ReadString('MSSQLUSR');
MS_SQLPwd:=ReadString('MSSQLPWD');
MS_SQLPwd:=_Crypt(MS_SQLPwd,65535,false);
DbConStr:=
'Provider=SQLOLEDB.1;Persist Security Info=True;'
+'User ID='+MS_SQLUsr+';Initial Catalog=Member;Data Source='+MS_SQLSERVER
+';Password='+MS_SQLPwd;
Oracle_SERVER:=ReadString('OracleSRV');
//获得Oracle相关参数:实例名、用户名、密码
Oracle_Usr:=ReadString('OracleUSR');
Oracle_Pwd:=ReadString('OraclePWD');
Oracle_Pwd:=_Crypt(Oracle_Pwd,65535,false);
//解密密码
FAdoConn:=TAdoconnection.Create(nil);
FAdoconn.ConnectionString:=Dbconstr;
FAdoconn.Connected:=True;
FQuery:=TAdoquery.Create(nil);
FAdoStoredProc:=TAdoStoredProc.Create(nil);
FAdoStoredProc.Connection:=FAdoconn;
Fquery.Connection:=Fadoconn;
With FQuerydo
//从Sql数据库中获得调用各个存储过程的时间
begin
SQL.Add('select importtime,sumnetage from t_sysinfo');
Open;
ImportTime:=formatdatetime('hh:mm:ss',Fields[0].AsDateTime);
sumnetage:=Fields[1].AsString;
Close;
end;
finally
Reg.free;
end;
end;

end;

procedure TImport.DisConnectDb;
begin
FQuery.Close;
FQuery.Free;
FAdoStoredProc.Close;
FAdoStoredProc.Free;
FAdoconn.Connected:=False;
FAdoConn.Free;
end;

destructor TImport.Destroy;
begin
DisConnectDb;
CoUnInitialize;
inherited Destroy;
end;

procedure TImport.Execute;
var
Month,Year,Day,Hour,Min,Sec,MSec: Word;
CurDay,CurTime:String;
begin
{ Place thread code here }
{每次数据导入执行时间}
ConnectDb;
while not Terminateddo
begin
DecodeTime(Time,Hour,Min,Sec,MSec);
DecodeDate(Date,Year,Month,Day);
CurDay:=inttostr(Dayof(now));
CurTime:=Format('%-2.2d:%-2.2d',[Hour,Min])+':00';
if Trim(CurTime)=ImportTime then
//如果到导入时间
begin
{导入数据代码}
with FAdoStoredProcdo
//以下代码是执行数据库中各个存储过程的代码
begin
Close;
ProcedureName:='sp_addlinkersrv';
Prepared:=true;
Parameters.Refresh;
Parameters[1].Value:=Oracle_SERVER;
Parameters[2].Value:=Oracle_Usr;
Parameters[3].Value:=Oracle_Pwd;
ExecProc;
if (Parameters[0].Value)>0 then
begin
//导入数据
Close;
ProcedureName:='sp_importdata';
Prepared:=true;
ExecProc;
//计算分数
Close;
ProcedureName:='sp_sumbonus';
Prepared:=true;
ExecProc;
Close;
end;
end;
end;
if (inttostr(Month)+'-'+inttostr(day))=sumnetage then
with FAdoStoredProcdo
begin
ProcedureName:='sp_importdata';
Prepared:=true;
ExecProc;
Close;
end;
Sleep(60000);
end;
end;
end.

我是头一次写服务程序,代码都是从以前大富翁中别人的贴子中参照编写的,所以非常希望有写过服务程序或多线程的高手帮我解决一下上面的难题,只要能帮我解决,分不是问题!
 
写了一个数据导入服务程序,实现的功能是将远程的oracle中的数据导入到本地的sql server数据库中,服务能正常启动,却怎么都不能终止,贴出服务线程的代码,请高手帮我看看是怎么一回事!
unit Unit_Import;
interface
uses
Classes,Sysutils,DB, ADODB,forms,ActiveX,Windows,Dialogs,DateUtils,Registry;
type
TImport = class(TThread)
private
FAdoConn:TAdoconnection;
FAdoStoredProc:TAdoStoredProc;
FQuery:TAdoquery;
ImportTime,SumNetAge:String;
{ Private declarations }
protected
procedure ConnectDb;
//连接数据库过程
procedure DisConnectDb;
//断开数据库过程
procedure Execute;
override;
public
destructor Destroy;override;
end;

var
Importthread: TImport;
DbConStr,MS_SQLSERVER,MS_SQLUsr,MS_SQLPwd:String;
Oracle_SERVER,Oracle_Usr,Oracle_Pwd:String;
implementation
uses FunCrypt;
procedure TImport.ConnectDb;
var
Reg:TRegistry;
begin
//服务启动时,将数据库参数获得
CoInitialize(nil);
Reg:=TRegistry.Create;
with Regdo
begin
try
RootKey := HKEY_LOCAL_MACHINE;
OpenKey('SOFTWARE/我的软件',False);
MS_SQLSERVER:= ReadString('MSSQLSRV');
MS_SQLUsr:=ReadString('MSSQLUSR');
MS_SQLPwd:=ReadString('MSSQLPWD');
MS_SQLPwd:=_Crypt(MS_SQLPwd,65535,false);
DbConStr:=
'Provider=SQLOLEDB.1;Persist Security Info=True;'
+'User ID='+MS_SQLUsr+';Initial Catalog=Member;Data Source='+MS_SQLSERVER
+';Password='+MS_SQLPwd;
Oracle_SERVER:=ReadString('OracleSRV');
//获得Oracle相关参数:实例名、用户名、密码
Oracle_Usr:=ReadString('OracleUSR');
Oracle_Pwd:=ReadString('OraclePWD');
Oracle_Pwd:=_Crypt(Oracle_Pwd,65535,false);
//解密密码
FAdoConn:=TAdoconnection.Create(nil);
FAdoconn.ConnectionString:=Dbconstr;
FAdoconn.Connected:=True;
FQuery:=TAdoquery.Create(nil);
FAdoStoredProc:=TAdoStoredProc.Create(nil);
FAdoStoredProc.Connection:=FAdoconn;
Fquery.Connection:=Fadoconn;
With FQuerydo
//从Sql数据库中获得调用各个存储过程的时间
begin
SQL.Add('select importtime,sumnetage from t_sysinfo');
Open;
ImportTime:=formatdatetime('hh:mm:ss',Fields[0].AsDateTime);
sumnetage:=Fields[1].AsString;
Close;
end;
finally
Reg.free;
end;
end;

end;

procedure TImport.DisConnectDb;
begin
FQuery.Close;
FQuery.Free;
FAdoStoredProc.Close;
FAdoStoredProc.Free;
FAdoconn.Connected:=False;
FAdoConn.Free;
end;

destructor TImport.Destroy;
begin
DisConnectDb;
CoUnInitialize;
inherited Destroy;
end;

procedure TImport.Execute;
var
Month,Year,Day,Hour,Min,Sec,MSec: Word;
CurDay,CurTime:String;
begin
{ Place thread code here }
{每次数据导入执行时间}
ConnectDb;
while not Terminateddo
begin
DecodeTime(Time,Hour,Min,Sec,MSec);
DecodeDate(Date,Year,Month,Day);
CurDay:=inttostr(Dayof(now));
CurTime:=Format('%-2.2d:%-2.2d',[Hour,Min])+':00';
if Trim(CurTime)=ImportTime then
//如果到导入时间
begin
{导入数据代码}
with FAdoStoredProcdo
//以下代码是执行数据库中各个存储过程的代码
begin
Close;
ProcedureName:='sp_addlinkersrv';
Prepared:=true;
Parameters.Refresh;
Parameters[1].Value:=Oracle_SERVER;
Parameters[2].Value:=Oracle_Usr;
Parameters[3].Value:=Oracle_Pwd;
ExecProc;
if (Parameters[0].Value)>0 then
begin
//导入数据
Close;
ProcedureName:='sp_importdata';
Prepared:=true;
ExecProc;
//计算分数
Close;
ProcedureName:='sp_sumbonus';
Prepared:=true;
ExecProc;
Close;
end;
end;
end;
if (inttostr(Month)+'-'+inttostr(day))=sumnetage then
with FAdoStoredProcdo
begin
ProcedureName:='sp_importdata';
Prepared:=true;
ExecProc;
Close;
end;
Sleep(60000);
end;
end;
end.

我是头一次写服务程序,代码都是从以前大富翁中别人的贴子中参照编写的,所以非常希望有写过服务程序或多线程的高手帮我解决一下上面的难题,只要能帮我解决,分不是问题!
 
while not Terminateddo
你没有让线程结束,没有设置Terminated的值,自然就永远运行了.
 
但这个线程只有当我停止服务时才能终止啊,否则就应该一直处于每隔一分钟就去检查的状态,所以我不知应该在哪里设置让Terminated变成false
 
运行一个过程后,提示一下!提示后终止。easy
 
没弄懂你的意思,请说具体一点,谢谢!
 
这是一个 死循环,要从外部调用:XXXThread.Terminate;
 
看看这个帖子:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2149950
 
后退
顶部