\documentclass[a4paper, 11pt]{article}
\renewcommand{\familydefault}{\sfdefault}
\usepackage{a4wide}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{color}
\usepackage{graphicx}
\usepackage{setspace}
\usepackage{ulem}
\usepackage{listings}
\usepackage{float}
\usepackage[rflt]{floatflt}
\usepackage{tabularx}
\setlength{\parindent}{0pt}
\title{Daemon (Service)}
\usepackage[
pdftitle={Daemon (Service)}
pdfsubject={Daemon (Service) Beispiel in Free Pascal}
pdfauthor={Udo Schmal}
pdfkeywords={Daemon, Free Pascal, Lazarus}
]{hyperref}
\definecolor{darkblue}{rgb}{0,0,.5}
\hypersetup{pdftex=true, colorlinks=true, breaklinks=true, linkcolor=darkblue, menucolor=darkblue, pagecolor=darkblue, urlcolor=darkblue}
\begin{document}Daemon (Service)
\singlespacing
{\LARGE \textbf{\label{sec:title}Daemon oder Windows System Service}}

Daemon oder auch Service Routinen sind mit Free Pascal siehe \href{https://wiki.freepascal.org/Daemons_and_Services}{wiki.freepascal.org/Daemons\_and\_Services} leicht zu realisieren, hier ein kleines Beispiel inklusive EventLog-Ausgabe.

Eine Variante in der FreeOnTerminate des Threads auf true gesetzt ist, wodurch im Daemon.Stop einfach mit Terminate das beenden des Threads eingeleitet werden kann und mit WaitFor auf das Ende gewartet werden kann. \newline


\href{http://www.gocher.me/code/thedaemon.lpr}{thedaemon.lpr}

Ausgabe:
\begin{lstlisting}[breaklines]\&gt; thedaemon.exe --install
thedaemon [2016-12-01 19:31:02.891 Debug] DaemonMapper.Create
thedaemon [2016-12-01 19:31:02.891 Debug] DaemonMapper.Createted
thedaemon [2016-12-01 19:31:02.891 Debug] DaemonMapper.Install
thedaemon [2016-12-01 19:31:02.907 Debug] Daemon.installed: true

\&gt; net start theDaemon
thedaemon [2016-12-01 19:31:05.360 Debug] DaemonMapper.Create
thedaemon [2016-12-01 19:31:05.360 Debug] DaemonMapper.Createted
thedaemon [2016-12-01 19:31:05.360 Debug] DaemonMapper.Run
thedaemon [2016-12-01 19:31:05.360 Info] Daemon TheDaemon current status: Start Pending
thedaemon [2016-12-01 19:31:05.360 Debug] Daemon.Start: true
thedaemon [2016-12-01 19:31:05.360 Info] Daemon TheDaemon current status: Running
thedaemon [2016-12-01 19:31:05.360 Debug] Daemon.Execute: false
thedaemon [2016-12-01 19:31:05.360 Debug] Thread.Execute
thedaemon [2016-12-01 19:31:06.376 Debug] Thread.Loop Tick :1
thedaemon [2016-12-01 19:31:07.379 Debug] Thread.Loop Tick :2
thedaemon [2016-12-01 19:31:08.382 Debug] Thread.Loop Tick :3

\&gt; net pause theDaemon
thedaemon [2016-12-01 19:31:08.820 Info] Daemon TheDaemon current status: Pause Pending
thedaemon [2016-12-01 19:31:08.820 Debug] Daemon.Pause: true
thedaemon [2016-12-01 19:31:08.820 Info] Daemon TheDaemon current status: Paused

\&gt; net continue theDaemon
thedaemon [2016-12-01 19:31:12.655 Info] Daemon TheDaemon current status: Continue Pending
thedaemon [2016-12-01 19:31:12.655 Debug] Daemon.Continue: true
theDaemon [2016-12-01 19:31:12.655 Info] Daemon TheDaemon current status: Running
theDaemon [2016-12-01 19:31:13.224 Debug] Thread.Loop Tick :4
theDaemon [2016-12-01 19:31:14.239 Debug] Thread.Loop Tick :5
theDaemon [2016-12-01 19:31:15.255 Debug] Thread.Loop Tick :6
theDaemon [2016-12-01 19:31:16.256 Debug] Thread.Loop Tick :7

\&gt; net stop theDaemon
theDaemon [2016-12-01 19:31:16.725 Info] Daemon TheDaemon current status: Stop Pending
theDaemon [2016-12-01 19:31:16.725 Debug] Daemon.Stop: true
thedaemon [2016-12-01 19:31:17.272 Debug] Thread.Loop Tick :8
thedaemon [2016-12-01 19:31:17.272 Debug] Thread.LoopStopped
thedaemon [2016-12-01 19:31:17.272 Debug] Thread.Destroy
theDaemon [2016-12-01 19:31:17.272 Info] Daemon TheDaemon current status: Stopped

\&gt; thedaemon.exe --uninstall
theDaemon [2016-12-01 19:31:20.198 Debug] DaemonMapper.Create
theDaemon [2016-12-01 19:31:20.198 Debug] DeamonMapper.Createted
theDaemon [2016-12-01 19:31:20.214 Debug] DeamonMapper.Uninstall
theDaemon [2016-12-01 19:31:20.214 Debug] Daemon.Uninstall: true\end{lstlisting}
\end{document}