最近需要写一个Delphi的系统服务,就是要求能够在计算机启动时就可以直接运行,而不依赖于每个系统用户。查了资料,并认真实践了一下,总结如下:

      打开Delphi 7,新建,选择Other->Service Application 修改属性中Name和DisplayName,Name是Service的名称,决定着进程的标识,DisplayName是显示在操作系统里的服务面板里的服务的名称。例如我们将Name修改为NoticeService。

      新建一个类,例如名字叫做TNoticeThread,继承自TThread,并重写protected过程Execute。在NoticeService的类定义中添加一个NoticeThread对象。

      找到属性的Events选项卡,分别添加OnStart,OnPause,OnContinue,OnStop事件,用这几个事件来控制NoticeThread线程的运行:

      代码如下:

  

 1Windows下使用Delphi编写系统服务unit Unit1;
 2Windows下使用Delphi编写系统服务
 3Windows下使用Delphi编写系统服务interface
 4Windows下使用Delphi编写系统服务
 5Windows下使用Delphi编写系统服务uses
 6Windows下使用Delphi编写系统服务  Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs;
 7Windows下使用Delphi编写系统服务
 8Windows下使用Delphi编写系统服务type
 9Windows下使用Delphi编写系统服务  TNoticeThread = class(TThread)
10Windows下使用Delphi编写系统服务  private
11Windows下使用Delphi编写系统服务  protected
12Windows下使用Delphi编写系统服务    procedure Execute; override;
13Windows下使用Delphi编写系统服务  end;
14Windows下使用Delphi编写系统服务
15Windows下使用Delphi编写系统服务  TNoticeService = class(TService)
16Windows下使用Delphi编写系统服务    procedure ServiceStart(Sender: TService; var Started: Boolean);
17Windows下使用Delphi编写系统服务    procedure ServicePause(Sender: TService; var Paused: Boolean);
18Windows下使用Delphi编写系统服务    procedure ServiceContinue(Sender: TService; var Continued: Boolean);
19Windows下使用Delphi编写系统服务    procedure ServiceStop(Sender: TService; var Stopped: Boolean);
20Windows下使用Delphi编写系统服务  private
21Windows下使用Delphi编写系统服务    NoticeThread: TNoticeThread;
22Windows下使用Delphi编写系统服务    { Private declarations }
23Windows下使用Delphi编写系统服务  public
24Windows下使用Delphi编写系统服务    function GetServiceController: TServiceController; override;
25Windows下使用Delphi编写系统服务    { Public declarations }
26Windows下使用Delphi编写系统服务  end;
27Windows下使用Delphi编写系统服务
28Windows下使用Delphi编写系统服务var
29Windows下使用Delphi编写系统服务  NoticeService: TNoticeService;
30Windows下使用Delphi编写系统服务
31Windows下使用Delphi编写系统服务implementation
32Windows下使用Delphi编写系统服务
33Windows下使用Delphi编写系统服务{$R *.DFM}
34Windows下使用Delphi编写系统服务
35Windows下使用Delphi编写系统服务procedure ServiceController(CtrlCode: DWord); stdcall;
36Windows下使用Delphi编写系统服务begin
37Windows下使用Delphi编写系统服务  NoticeService.Controller(CtrlCode);
38Windows下使用Delphi编写系统服务end;
39Windows下使用Delphi编写系统服务
40Windows下使用Delphi编写系统服务function TNoticeService.GetServiceController: TServiceController;
41Windows下使用Delphi编写系统服务begin
42Windows下使用Delphi编写系统服务  Result := ServiceController;
43Windows下使用Delphi编写系统服务end;
44Windows下使用Delphi编写系统服务
45Windows下使用Delphi编写系统服务procedure TNoticeService.ServiceStart(Sender: TService; var Started: Boolean);
46Windows下使用Delphi编写系统服务begin
47Windows下使用Delphi编写系统服务  NoticeThread := TNoticeThread.Create(False);
48Windows下使用Delphi编写系统服务  Started := True;
49Windows下使用Delphi编写系统服务end;
50Windows下使用Delphi编写系统服务
51Windows下使用Delphi编写系统服务procedure TNoticeService.ServicePause(Sender: TService; var Paused: Boolean);
52Windows下使用Delphi编写系统服务begin
53Windows下使用Delphi编写系统服务  NoticeThread.Suspend;
54Windows下使用Delphi编写系统服务  Paused := True;
55Windows下使用Delphi编写系统服务end;
56Windows下使用Delphi编写系统服务
57Windows下使用Delphi编写系统服务procedure TNoticeService.ServiceContinue(Sender: TService;
58Windows下使用Delphi编写系统服务  var Continued: Boolean);
59Windows下使用Delphi编写系统服务begin
60Windows下使用Delphi编写系统服务  NoticeThread.Resume;
61Windows下使用Delphi编写系统服务  Continued := True;
62Windows下使用Delphi编写系统服务end;
63Windows下使用Delphi编写系统服务
64Windows下使用Delphi编写系统服务procedure TNoticeService.ServiceStop(Sender: TService; var Stopped: Boolean);
65Windows下使用Delphi编写系统服务begin
66Windows下使用Delphi编写系统服务  NoticeThread.Terminate;
67Windows下使用Delphi编写系统服务  Stopped := True;
68Windows下使用Delphi编写系统服务end;
69Windows下使用Delphi编写系统服务
70Windows下使用Delphi编写系统服务procedure TNoticeThread.Execute;
71Windows下使用Delphi编写系统服务begin
72Windows下使用Delphi编写系统服务  //do what the notice service will do
73Windows下使用Delphi编写系统服务end;  
74Windows下使用Delphi编写系统服务
75Windows下使用Delphi编写系统服务end.
76Windows下使用Delphi编写系统服务

 

      当然,可以将NoticeThread的定义放在另外一个Unit里即可,如果这样,只需在新建Service Application后,选择新建,Other->Thread Object即可。

      也并不一定非得用一个Thread来控制服务,只要符合需求即可。

相关文章:

  • 2021-10-03
  • 2022-01-31
  • 2021-08-16
  • 2022-02-21
  • 2021-09-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-08
  • 2022-12-23
  • 2022-12-23
  • 2021-07-09
  • 2022-12-23
相关资源
相似解决方案