File: daemonworkerthread.pas

package info (click to toggle)
lazarus 4.6%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 276,392 kB
  • sloc: pascal: 2,344,754; xml: 509,184; makefile: 355,815; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (85 lines) | stat: -rw-r--r-- 2,377 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
unit DaemonWorkerThread;

{$mode ObjFPC}{$H+}

interface

uses
  Classes, SysUtils;

// --------------------------------------------------------------------------------
// This is the "workhorse" of the daemon, and just a normal therad, see the Lazarus
// docs about threads and mutlitasking for details.

// Execute holds the main work code of the service
// Do not try the execute method of TDaemon, since it does not multitask,
// the service thread will stop responding to control messages if looping in the
// TDaemon execute method. Thus we need a worker thread.
// --------------------------------------------------------------------------------

type
  TDaemonWorkerThread = class(TThread)
  private
  public
    procedure Execute; override;  // the actual worker thread code goes here
    constructor Create;
    destructor Destroy; override;
  end;

implementation

uses DaemonUnit;

// --------------------------------------------------------------------------
// TThread.execute: this is the core of the workhorse, the routine which does
// actually hold the service's working code
// --------------------------------------------------------------------------

procedure TDaemonWorkerThread.Execute;

var
  i: integer;

begin
  LogToFile('Daemon worker thread executing');
  while not Terminated do
  begin
    // placeholder, put your actual service code here
    // ...
    LogToFile('Daemon worker thread running');
    // Thread- and CPUload friendly 5s delay loop
    for i := 1 to 50 do
    begin
      if Terminated then break;
      Sleep(100);
    end;
    // ...
    // ----------------------------------------
  end;
  LogToFile('Daemon worker thread terminated');
end;

// -------------------------------------------------
// Construction and destruction of the worker thread
// -------------------------------------------------

constructor TDaemonWorkerThread.Create;

begin
  // Create a suspended worker thread to allow further parametrizon before it
  // does actually start
  // The thread will be started if the OS sends a "Start" Signal to TDaemon
  // See OnStart event handler of the TDeamon class
  inherited Create(True);
  LogToFile('Daemon worker thread created');
end;

destructor TDaemonWorkerThread.Destroy;

begin
  // Nothing to do here, just for logging
  LogToFile('Daemon worker thread destroyed');
  inherited Destroy;
end;

end.