From 49b5ebddb18f50b0c126f81bd28fd3dbe1ef4324 Mon Sep 17 00:00:00 2001 From: Pilzinsel64 Date: Tue, 28 Jan 2025 08:47:55 +0100 Subject: [PATCH] don't error when stopping --- Pilz.Net/Api/ApiServer.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Pilz.Net/Api/ApiServer.cs b/Pilz.Net/Api/ApiServer.cs index 3425efc..eb84e78 100644 --- a/Pilz.Net/Api/ApiServer.cs +++ b/Pilz.Net/Api/ApiServer.cs @@ -17,6 +17,7 @@ public class ApiServer(string apiUrl) : IApiServer protected int restartAttempts = 0; protected DateTime lastRestartAttempt; protected SemaphoreSlim? semaphore; + protected bool doListen; public event OnCheckAuthenticationEventHandler? OnCheckAuthentication; public event OnCheckContextEventHandler? OnCheckContext; @@ -52,6 +53,7 @@ public class ApiServer(string apiUrl) : IApiServer { Log.Info("Starting listener"); httpListener.Prefixes.Add(ApiUrl + "/"); + doListen = true; httpListener.Start(); Log.Info("Started listener"); Receive(); @@ -60,6 +62,7 @@ public class ApiServer(string apiUrl) : IApiServer public virtual void Stop() { Log.Info("Stopping listener"); + doListen = false; httpListener.Stop(); Thread.Sleep(StopDelay); httpListener.Close(); @@ -181,7 +184,7 @@ public class ApiServer(string apiUrl) : IApiServer HttpListenerContext? context; // Skip if not lisstening anymore - if (!httpListener.IsListening) + if (!httpListener.IsListening || !doListen) return; // Wait for a free slot