Compare commits

...

3 Commits

Author SHA1 Message Date
Imi ba3107d03c
Create README.md 2023-01-25 20:45:00 +01:00
Imi c1fc82021e
Update Program.cs 2023-01-25 20:43:03 +01:00
Imi 271e654489
Create Program.cs 2023-01-25 20:41:50 +01:00
2 changed files with 62 additions and 0 deletions

2
README.md Normal file
View File

@ -0,0 +1,2 @@
# This repo is for testing, developing and bug tracking only.
Please do not fork or contribute on it.

View File

@ -0,0 +1,60 @@
// ==============================================================================================
// For testing windows service with .NET 5
// Source: https://stackoverflow.com/questions/7764088/net-console-application-as-windows-service
// ==============================================================================================
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
}
}