public HandleReadEventChanger(ITerminalInputStream consoleInput, ConsoleReadHandler readHandler) { this.consoleInput = consoleInput; this.previousReadHandler = consoleInput.ReadHandler; consoleInput.ReadHandler = readHandler; }
NonBlockingConsole() { // unit tests if (Core.Portable.PlatformInfo.System.IsRunningFromNUnit) { running = false; IsInputOpen = false; } // normal program else { running = true; threadOutput = new Thread( () => { runningOutput = true; string item; while (running) { if (queueOutput.TryTake(out item, InputInterval)) { lock (lockObject) { Console.Write(item); } } } runningOutput = false; }); threadOutput.IsBackground = true; threadOutput.Start(); threadInput = new Thread( () => { runningInput = true; while (running) { while (Console.KeyAvailable) { lock (lockObject) { queueInput.Add(Console.ReadKey(true)); } } Thread.Sleep(InputInterval); } runningInput = false; }); threadInput.IsBackground = true; threadInput.Start(); threadInputProcessing = new Thread( () => { runningInputProcessing = true; ConsoleKeyInfo item; ITerminalInputStream self = this; while (running) { if (queueInput.TryTake(out item, InputInterval)) { //Log.Debug ("before input process"); self.ReadHandler(new PortableConsoleKeyInfo { Modifiers = (Core.IO.Terminal.ConsoleModifiers)item.Modifiers, Key = (Core.IO.Terminal.ConsoleKey)item.Key, KeyChar = item.KeyChar, }).Wait(); //Log.Debug ("after input process"); } } runningInputProcessing = false; }); threadInputProcessing.IsBackground = true; threadInputProcessing.Start(); IsInputOpen = Core.Portable.PlatformInfo.System.IsInteractive; } }
public static IDisposable HandleReadEvent(this ITerminalInputStream consoleInput, ConsoleReadHandler readHandler) { return(new HandleReadEventChanger(consoleInput: consoleInput, readHandler: readHandler)); }