diff --git a/source/Cosmos.System2_Plugs/System/ConsoleImpl.cs b/source/Cosmos.System2_Plugs/System/ConsoleImpl.cs index 3c6af9d5ce..a36a0e6c06 100644 --- a/source/Cosmos.System2_Plugs/System/ConsoleImpl.cs +++ b/source/Cosmos.System2_Plugs/System/ConsoleImpl.cs @@ -13,7 +13,9 @@ public static class ConsoleImpl private static ConsoleColor mForeground = ConsoleColor.White; private static ConsoleColor mBackground = ConsoleColor.Black; private static Encoding ConsoleInputEncoding = Encoding.ASCII; - private static Encoding ConsoleOutputEncoding = Encoding.ASCII; + private static Encoding ConsoleOutputEncoding = Encoding.ASCII; + private static Core.Processing.Mutex mConsoleGateRead = new Core.Processing.Mutex(); + private static Core.Processing.Mutex mConsoleGateWrite = new Core.Processing.Mutex(); private static readonly Cosmos.System.Console mFallbackConsole = new Cosmos.System.Console(null); @@ -429,17 +431,20 @@ public static ConsoleKeyInfo ReadKey(bool intercept) bool xControl = (key.Modifiers & ConsoleModifiers.Control) == ConsoleModifiers.Control; return new ConsoleKeyInfo(key.KeyChar, key.Key.ToConsoleKey(), xShift, xAlt, xControl); - } - + } + + private static List chars = new List(32); public static String ReadLine() { + if (mConsoleGateRead != null) + mConsoleGateRead.Lock(); + chars = new List(32); var xConsole = GetConsole(); if (xConsole == null) { // for now: return null; } - List chars = new List(32); KeyEvent current; int currentCount = 0; @@ -529,6 +534,8 @@ public static String ReadLine() WriteLine(); char[] final = chars.ToArray(); + if (mConsoleGateRead != null) + mConsoleGateRead.Unlock(); return new string(final); } @@ -601,17 +608,21 @@ public static void Write(bool aBool) /* Correct behaviour printing null should not throw NRE or do nothing but should print an empty string */ public static void Write(object value) => Write((value ?? String.Empty)); + private static Cosmos.System.Console xConsole = null; + public static void Write(string aText) { - var xConsole = GetConsole(); - if (xConsole == null) - { - // for now: - return; + if (mConsoleGateWrite != null) + { + mConsoleGateWrite.Lock(); } - byte[] aTextEncoded = ConsoleOutputEncoding.GetBytes(aText); GetConsole().Write(aTextEncoded); + //if there's some process queued, we just set the first as the main and queue the other ones + if (mConsoleGateWrite != null) + { + mConsoleGateWrite.Unlock(); + } } public static void Write(uint aInt) => Write(aInt.ToString());