Skip to content

Commit

Permalink
Service fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
NTDLS committed Feb 1, 2024
1 parent 097d389 commit 9247e27
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions NTDLS.Katzebase.Server/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NTDLS.Katzebase.Server;
using Topshelf;
using Topshelf.ServiceConfigurators;

namespace NTDLS.Katzebase.Server
{
Expand All @@ -10,7 +11,7 @@ public class KatzebaseService
private SemaphoreSlim _semaphoreToRequestStop;
private Thread _thread;

public KatzebaseService()
public KatzebaseService(ServiceConfigurator< KatzebaseService> s )
{
_semaphoreToRequestStop = new SemaphoreSlim(0);
_thread = new Thread(DoWork);
Expand All @@ -29,20 +30,27 @@ public void Stop()

private void DoWork()
{
var apiService = new APIService();
try
{
var apiService = new APIService();

apiService.Start();
apiService.Start();

while (true)
{
if (_semaphoreToRequestStop.Wait(500))
while (true)
{
apiService.Stop();
break;
if (_semaphoreToRequestStop.Wait(500))
{
apiService.Stop();
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occured while starting or the service: {ex.Message}");
return;
}
}

}

public static void Main()
Expand All @@ -58,7 +66,7 @@ public static void Main()
x.Service<KatzebaseService>(s =>
{
s.ConstructUsing(hostSettings => new KatzebaseService());
s.ConstructUsing(hostSettings => new KatzebaseService(s));
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
Expand Down

0 comments on commit 9247e27

Please sign in to comment.