windows 服务程序和桌面程序集成(四)桌面程序

系列文章目录链接:

  1. windows 服务程序和桌面程序集成(一)概念介绍
  2. windows 服务程序和桌面程序集成(二)服务程序
  3. windows 服务程序和桌面程序集成(三)UDP监控工具
  4. windows 服务程序和桌面程序集成(四)桌面程序
  5. windows 服务程序和桌面程序集成(五)集成为一个EXE
  6. windows 服务程序和桌面程序集成(六)集成安装、启动、卸载功能
  7. windows 服务程序和桌面程序集成(七)效果演示及源程序下载
     

对于VCL桌面程序就非常简单,如何创建一级如何运行就不在啰嗦。

这个桌面程序就是和 windows 服务程序和桌面程序集成(二)服务程序 共享Public目录下的uWorkThread.pas单元。

程序中的4个按键就是围绕 uWorkerThread.pas 单元设置的。这个程序目前和 windows 服务程序和桌面程序集成(二)服务程序 中的服务程序没有直接关系,理解就是一个简单的VCL桌面程序,调用一个线程发送UDP消息而已。他们共享了线程单元uWorkerThread.pas,这个是重点!

 uMainForm.pas代码如下:

unit uMainForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  uWorkerThread;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  //创建工作线程
  if WorkThread = nil then
     WorkThread := TWorkThread.Create;
  WorkThread.FreeOnTerminate := True;    //完成后直接释放
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
 WorkThread.Pause;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  WorkThread.Continue;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  WorkThread.Terminate;
  while not WorkThread.Finished do
    begin
      Sleep(200);
    end;
  WorkThread := nil;

end;

end.

uWorkerThread.pas代码:

unit uWorkerThread;

interface
uses
  System.Classes,
  IdUDPClient,
  IdGlobal,
  System.SysUtils;
  //Winapi.Windows;

type
  //实际工作线程类
  TWorkThread = Class(TThread)
     private
       FPaused : Boolean;   //
     protected
       constructor Create;
       procedure Execute; override;
     public
       procedure Pause;
       procedure Continue;
  End;
//服务执行的函数,UDP发送消息函数
procedure Send_UDP_Info(str : string);

var
  //工作线程变量
  WorkThread : TWorkThread;

implementation

procedure Send_UDP_Info(str : string);
var
  UDPClient: TIdUDPClient;
  B : TBytes;
begin
  UDPClient := TIdUDPClient.Create(nil);
  try
    UDPClient.BroadcastEnabled := True;
    B := TEncoding.UTF8.GetBytes(str);
    //只给本机发送,这个地方只需要给本机发送广播消息即可 2023-03-04
    UDPClient.Broadcast(TidBytes(B),8192,'127.0.0.1');  //端口号
    //广播到任何地方
    //UDPClient.Broadcast(TidBytes(B),G_UDPPort);  //端口号
  finally
    UDPClient.Free;
  end;
end;


{ TWorkThread }

procedure TWorkThread.Continue;
begin
  FPaused := False;
  Send_UDP_Info('服务继续工作....');
end;

constructor TWorkThread.Create;
begin
  FPaused := False;
end;

procedure TWorkThread.Execute;
var
  S : string;
begin
  inherited;
  while not Terminated do
  begin
    if not FPaused then
       begin
         S := FormatDateTime('YYYY-MM-DD hh:mm:ss',Now);
         Send_UDP_Info(S);
       end;
    TThread.Sleep(1000);
  end;

  Send_UDP_Info('********** 服务终止工作 **********');
end;

procedure TWorkThread.Pause;
begin
  FPaused := True;
  Send_UDP_Info('服务暂停工作!!!');
end;

end.

下一篇:windows 服务程序和桌面程序集成(五)集成为一个EXE

猜你喜欢

转载自blog.csdn.net/sensor_WU/article/details/131155478
今日推荐