From 1e934c9877e173dc2edc28bf15a3d2147bd14def Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Wed, 28 Oct 2020 14:42:30 -0400 Subject: [PATCH] fix: Added critical section to IcdConsole to clean up multi-threaded output --- ICD.Common.Utils/IcdConsole.cs | 42 +++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/ICD.Common.Utils/IcdConsole.cs b/ICD.Common.Utils/IcdConsole.cs index 92102c9..23c0a15 100644 --- a/ICD.Common.Utils/IcdConsole.cs +++ b/ICD.Common.Utils/IcdConsole.cs @@ -22,6 +22,16 @@ namespace ICD.Common.Utils public static event EventHandler OnConsolePrint; + private static readonly SafeCriticalSection s_Section; + + /// + /// Static constructor. + /// + static IcdConsole() + { + s_Section = new SafeCriticalSection(); + } + /// /// Wraps CrestronConsole.ConsoleCommandResponse for S+ compatibility. /// @@ -64,12 +74,22 @@ namespace ICD.Common.Utils public static void PrintLine(string message) { + s_Section.Enter(); + + try + { #if SIMPLSHARP - if (IcdEnvironment.RuntimeEnvironment != IcdEnvironment.eRuntimeEnvironment.SimplSharpProServer) - CrestronConsole.PrintLine(message); + if (IcdEnvironment.RuntimeEnvironment != IcdEnvironment.eRuntimeEnvironment.SimplSharpProServer) + CrestronConsole.PrintLine(message); #else - Console.WriteLine(message); + Console.WriteLine(message); #endif + } + finally + { + s_Section.Leave(); + } + OnConsolePrint.Raise(null, new StringEventArgs(message + IcdEnvironment.NewLine)); } @@ -93,12 +113,22 @@ namespace ICD.Common.Utils public static void Print(string message) { + s_Section.Enter(); + + try + { #if SIMPLSHARP - if (IcdEnvironment.RuntimeEnvironment != IcdEnvironment.eRuntimeEnvironment.SimplSharpProServer) - CrestronConsole.Print(message); + if (IcdEnvironment.RuntimeEnvironment != IcdEnvironment.eRuntimeEnvironment.SimplSharpProServer) + CrestronConsole.Print(message); #else - Console.Write(message); + Console.Write(message); #endif + } + finally + { + s_Section.Leave(); + } + OnConsolePrint.Raise(null, new StringEventArgs(message)); }