关于delphi服务程序问题
我原来在2000下做了一个服务程序,当时运行一切都正常
可是最近拿出来,在xp下运行时,发现无法启动服务,
跟踪调试,发现其根本无法执行ServiceStart方法,请问大侠是什么问题呀
现在即使弄到2000下,也无法执行
一下代码是几个相关的过程
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
TimeSrv.Controller(CtrlCode);
end;
function TTimeSrv.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TTimeSrv.ServiceStart(Sender: TService; var Started: Boolean);
var
th:TTLSThread;
begin
gPath := ExtractFilePath(ParamStr(0));
if gPath[length(gPath)] <> '\ ' then gPath := gPath+ '\ ';
WriteLog( '启动服务 ');
th := TTLSThread.Create(true);
th.Resume;
Started := true;
end;
procedure TTimeSrv.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
WriteLog( '停止服务 ');
WriteLog( ' ');
end;
[解决办法]
使用delphi的VCL类 TServiceApplication,TService这两个类来写NT的服务程序时
1.如果在TService的ServiceStart事件中不创建一个线程,那么就在OnExecute事件中编写服务程序的处理代码,当前OnExecute结束时,ServiceThread就结束,而服务程序的运行就终止了
2.就是楼主的情况,在TService的ServiceStart事件中创建一个子线程,这里就应该在OnExecute中等待该子线程的运行,而ServiceThread 则调用ProcessRequests这个函响应系统的处理珳求即可
通常来说,第2种是比较常用的方法。可以在ServiceStart中创建抛出多个线程,来处理服务程序的业务,这样做相对来说对于扩展性,稳定性,效率上都是比较好的选择
回到上面的问题,如果在刚才始2000下可以,xp不可以,修改后却变成了xp可以,2000不可以
呵呵,好好检查代码吧