From 0d1d5c6490b0d83c09c8282a438be62c92eb37c1 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Fri, 7 Jul 2017 22:54:43 -0700 Subject: [PATCH] Fix catching listener exceptions --- Ooui/UI.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Ooui/UI.cs b/Ooui/UI.cs index b461203..db72c68 100644 --- a/Ooui/UI.cs +++ b/Ooui/UI.cs @@ -177,6 +177,7 @@ namespace Ooui static async Task RunAsync (string listenerPrefix, CancellationToken token) { HttpListener listener = null; + var wait = 5; started.Reset (); while (!started.WaitOne(0) && !token.IsCancellationRequested) { @@ -186,12 +187,18 @@ namespace Ooui listener.Start (); started.Set (); } - catch (System.Net.Sockets.SocketException ex) when - (ex.SocketErrorCode == System.Net.Sockets.SocketError.AddressAlreadyInUse) { - var wait = 5; - Console.WriteLine ($"{listenerPrefix} is in use, trying again in {wait} seconds..."); + catch (System.Net.Sockets.SocketException ex) { + Console.WriteLine ($"{listenerPrefix} error: {ex.Message}. Trying again in {wait} seconds..."); await Task.Delay (wait * 1000).ConfigureAwait (false); } + catch (System.Net.HttpListenerException ex) { + Console.WriteLine ($"{listenerPrefix} error: {ex.Message}. Trying again in {wait} seconds..."); + await Task.Delay (wait * 1000).ConfigureAwait (false); + } + catch (Exception ex) { + Error ("Error listening", ex); + return; + } } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine ($"Listening at {listenerPrefix}...");