From 271e654489b43610142c3c323c3cc0e4c1d092ee Mon Sep 17 00:00:00 2001 From: Imi <27138272+iminet@users.noreply.github.com> Date: Wed, 25 Jan 2023 20:41:50 +0100 Subject: [PATCH] Create Program.cs --- csharp/Dotnetservice/Program.cs | 54 +++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 csharp/Dotnetservice/Program.cs diff --git a/csharp/Dotnetservice/Program.cs b/csharp/Dotnetservice/Program.cs new file mode 100644 index 0000000..ecc7209 --- /dev/null +++ b/csharp/Dotnetservice/Program.cs @@ -0,0 +1,54 @@ +using System.ServiceProcess + +public static class Program +{ + #region Nested classes to support running as service + public const string ServiceName = "MyService"; + + public class Service : ServiceBase + { + public Service() + { + ServiceName = Program.ServiceName; + } + + protected override void OnStart(string[] args) + { + Program.Start(args); + } + + protected override void OnStop() + { + Program.Stop(); + } + } + #endregion + + static void Main(string[] args) + { + if (!Environment.UserInteractive) + // running as service + using (var service = new Service()) + ServiceBase.Run(service); + else + { + // running as console app + Start(args); + + Console.WriteLine("Press any key to stop..."); + Console.ReadKey(true); + + Stop(); + } + } + + private static void Start(string[] args) + { + // onstart code here + } + + private static void Stop() + { + // onstop code here + } +}