From c1e939f1e371634ebd033a919dc682e436eb6d86 Mon Sep 17 00:00:00 2001 From: glax Date: Tue, 29 Aug 2023 12:40:10 +0200 Subject: [PATCH] Server correct shutdown/force shutdown --- Tranga/Server.cs | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/Tranga/Server.cs b/Tranga/Server.cs index d0494f9..71b92c9 100644 --- a/Tranga/Server.cs +++ b/Tranga/Server.cs @@ -22,8 +22,17 @@ public class Server : GlobalBase this._listener.Prefixes.Add($"http://*:{settings.apiPortNumber}/"); else this._listener.Prefixes.Add($"http://localhost:{settings.apiPortNumber}/"); - Thread t = new (Listen); - t.Start(); + Thread listenThread = new (Listen); + listenThread.Start(); + Thread watchThread = new(WatchRunning); + watchThread.Start(); + } + + private void WatchRunning() + { + while(_parent.keepRunning) + Thread.Sleep(1000); + this._listener.Close(); } private void Listen() @@ -33,13 +42,20 @@ public class Server : GlobalBase Log($"Listening on {prefix}"); while (this._listener.IsListening && _parent.keepRunning) { - HttpListenerContext context = this._listener.GetContextAsync().Result; - Log($"{context.Request.HttpMethod} {context.Request.Url} {context.Request.UserAgent}"); - Task t = new(() => + try { - HandleRequest(context); - }); - t.Start(); + HttpListenerContext context = this._listener.GetContext(); + Log($"{context.Request.HttpMethod} {context.Request.Url} {context.Request.UserAgent}"); + Task t = new(() => + { + HandleRequest(context); + }); + t.Start(); + } + catch (HttpListenerException e) + { + + } } }