fix: Added critical section to IcdConsole to clean up multi-threaded output

This commit is contained in:
Chris Cameron
2020-10-28 14:42:30 -04:00
parent 64cc662ee7
commit 1e934c9877

View File

@@ -22,6 +22,16 @@ namespace ICD.Common.Utils
public static event EventHandler<StringEventArgs> OnConsolePrint; public static event EventHandler<StringEventArgs> OnConsolePrint;
private static readonly SafeCriticalSection s_Section;
/// <summary>
/// Static constructor.
/// </summary>
static IcdConsole()
{
s_Section = new SafeCriticalSection();
}
/// <summary> /// <summary>
/// Wraps CrestronConsole.ConsoleCommandResponse for S+ compatibility. /// Wraps CrestronConsole.ConsoleCommandResponse for S+ compatibility.
/// </summary> /// </summary>
@@ -64,12 +74,22 @@ namespace ICD.Common.Utils
public static void PrintLine(string message) public static void PrintLine(string message)
{ {
s_Section.Enter();
try
{
#if SIMPLSHARP #if SIMPLSHARP
if (IcdEnvironment.RuntimeEnvironment != IcdEnvironment.eRuntimeEnvironment.SimplSharpProServer) if (IcdEnvironment.RuntimeEnvironment != IcdEnvironment.eRuntimeEnvironment.SimplSharpProServer)
CrestronConsole.PrintLine(message); CrestronConsole.PrintLine(message);
#else #else
Console.WriteLine(message); Console.WriteLine(message);
#endif #endif
}
finally
{
s_Section.Leave();
}
OnConsolePrint.Raise(null, new StringEventArgs(message + IcdEnvironment.NewLine)); OnConsolePrint.Raise(null, new StringEventArgs(message + IcdEnvironment.NewLine));
} }
@@ -93,12 +113,22 @@ namespace ICD.Common.Utils
public static void Print(string message) public static void Print(string message)
{ {
s_Section.Enter();
try
{
#if SIMPLSHARP #if SIMPLSHARP
if (IcdEnvironment.RuntimeEnvironment != IcdEnvironment.eRuntimeEnvironment.SimplSharpProServer) if (IcdEnvironment.RuntimeEnvironment != IcdEnvironment.eRuntimeEnvironment.SimplSharpProServer)
CrestronConsole.Print(message); CrestronConsole.Print(message);
#else #else
Console.Write(message); Console.Write(message);
#endif #endif
}
finally
{
s_Section.Leave();
}
OnConsolePrint.Raise(null, new StringEventArgs(message)); OnConsolePrint.Raise(null, new StringEventArgs(message));
} }