From 7784b99f750b92af9c31628e3eca08083aef18e3 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Thu, 13 May 2021 12:04:44 -0400 Subject: [PATCH] fix: Logic for determining if there is a console output window, don't try to write to console if no console window --- ICD.Common.Utils/IcdConsole.cs | 48 +++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/ICD.Common.Utils/IcdConsole.cs b/ICD.Common.Utils/IcdConsole.cs index 7bb8362..98c05bf 100644 --- a/ICD.Common.Utils/IcdConsole.cs +++ b/ICD.Common.Utils/IcdConsole.cs @@ -25,9 +25,47 @@ namespace ICD.Common.Utils public static event EventHandler OnConsolePrint; private static readonly SafeCriticalSection s_Section; - private static readonly Regex s_NewLineRegex; + private static bool? s_IsConsoleApp; + + /// + /// Returns true if the application is being run from an interactive console, + /// false if the application is being run as a headless service. + /// + /// + public static bool IsConsoleApp + { + get + { + if (s_IsConsoleApp == null) + { +#if SIMPLSHARP + s_IsConsoleApp = true; +#else + try + { + // Hack + int unused = Console.WindowHeight; + s_IsConsoleApp = true; + + if (Console.Title.Length > 0) + s_IsConsoleApp = true; + + if (!Environment.UserInteractive) + s_IsConsoleApp = false; + } + catch + { + s_IsConsoleApp = false; + } +#endif + } + + return s_IsConsoleApp.Value; + } + } + /// /// Static constructor. /// @@ -92,7 +130,9 @@ namespace ICD.Common.Utils CrestronConsole.PrintLine(fixedMessage); #else Trace.WriteLine(AnsiUtils.StripAnsi(fixedMessage)); - Console.WriteLine(fixedMessage); + + if (IsConsoleApp) + Console.WriteLine(fixedMessage); #endif } finally @@ -134,7 +174,9 @@ namespace ICD.Common.Utils CrestronConsole.Print(fixedMessage); #else Trace.Write(AnsiUtils.StripAnsi(fixedMessage)); - Console.Write(fixedMessage); + + if (IsConsoleApp) + Console.Write(fixedMessage); #endif } finally