我使用向导创建了一个新的Windows Service
项目,提出了一些代码,编译它,运行它与/INSTALL
,然后我试图启动它使用net start myservice
但我有service name not found
错误;然后我去了服务中的控制面板,当我尝试开始点击“开始”链接时,显示的对话窗口无限期地冻结在进度条的50%处。为什么我的Delphi服务应用程序不启动?
这是我第一次尝试做一个服务来更新我正在开发的主系统,并且为了测试,我放了一个Timer
来告诉每一分钟的时间。任何人都可以注意到什么是错的,为什么它的行为如此呢?
的DPR
文件有:
{...}
begin
if not Application.DelayInitialize or Application.Installing then
begin
Application.Initialize;
end;
Application.CreateForm(TZeusUpdateSevice, ZeusUpdateSevice);
Application.Run;
end.
和PAS
文件用:
{...}
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
ZeusUpdateSevice.Controller(CtrlCode);
end;
function TZeusUpdateSevice.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TZeusUpdateSevice.ServiceAfterInstall(Sender: TService);
var
regEdit : TRegistry;
begin
regEdit := TRegistry.Create(KEY_READ or KEY_WRITE);
try
regEdit.RootKey := HKEY_LOCAL_MACHINE;
if regEdit.OpenKey('\SYSTEM\CurrentControlSet\Services\' + Name,False) then
begin
regEdit.WriteString('Description','Mantém atualizados os arquivos e as credenciais da Plataforma Zeus.');
regEdit.CloseKey;
end;
finally
FreeAndNil(regEdit);
end;
end;
procedure TZeusUpdateSevice.ServiceStart(Sender: TService; var Started: Boolean);
begin
{ executa os processos solicitados pelo sistema }
Timer1.Enabled := True;
while not Terminated do ServiceThread.ProcessRequests(True);
Timer1.Enabled := False;
end;
procedure TZeusUpdateSevice.Timer1Timer(Sender: TObject);
begin
ShowMessage('Now, time is: ' + TimeToStr(Now));
end;
看看[服务应用教程](http://www.tolderlund.eu/delphi/service/service.htm) –
在这里寻找一个基本的服务框架:http://stackoverflow.com/a/ 10538102/800214 – whosrdaddy
您应该使用线程而不是定时器 – whosrdaddy