RSS订阅 | 匿名投稿
您的位置:网站首页 > 系统 > 正文

Delphi 编写系统服务

作者:admin 来源: 日期:2013/3/25 16:01:07 人气: 标签:

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

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

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

      代码如下:

unit ufrmMain;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs;

type
  TNoticeThread = class(TThread)
  private

  protected
    procedure Execute; override;
  end;

type
  TNoticeService = class(TService)
    procedure ServiceStart(Sender: TService; var Started: Boolean);
    procedure ServiceStop(Sender: TService; var Stopped: Boolean);
    procedure ServicePause(Sender: TService; var Paused: Boolean);
    procedure ServiceContinue(Sender: TService; var Continued: Boolean);
  private
    NoticeThread: TNoticeThread;
  public
    function GetServiceController: TServiceController; override;
  end;

var
  NoticeService: TNoticeService;

implementation

{$R *.DFM}

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  NoticeService.Controller(CtrlCode);
end;

function TNoticeService.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;

procedure TNoticeService.ServiceStart(Sender: TService;
  var Started: Boolean);
begin
  NoticeThread := TNoticeThread.Create(False);
  Started := True;
end;

procedure TNoticeService.ServiceStop(Sender: TService;
  var Stopped: Boolean);
begin
  NoticeThread.Terminate;
  Stopped := True;
end;

procedure TNoticeService.ServicePause(Sender: TService;
  var Paused: Boolean);
begin
  NoticeThread.Suspend;
  Paused := True;
end;

procedure TNoticeService.ServiceContinue(Sender: TService;
  var Continued: Boolean);
begin
  NoticeThread.Resume;
  Continued := True;
end;

{ TNoticeThread }

procedure TNoticeThread.Execute;
begin
  inherited;

end;

end.


读完这篇文章后,您心情如何?
0
0
0
0
0
0
0
0
本文网址: