public bool ReadLine(out string input, int timeoutMilliseconds = Timeout.Infinite) { ReadLineState state = new ReadLineState() { GetInputEvent = new ManualResetEvent(false), GotInputEvent = new ManualResetEvent(false), InputThread = new Thread(ReadLoop) { IsBackground = true } }; state.InputThread.Start(state); // Trigger ReadLine(); state.GetInputEvent.Set(); var successful = state.GotInputEvent.WaitOne(timeoutMilliseconds); if (successful) { input = state.Input; return(true); } input = null; return(false); }
/// <summary> /// Read code and split into lines, removing comments. /// </summary> /// <returns>string[] the lines, sans comments</returns> protected string[] readLines() { int roughLineCount = Code.Count(c => c == '\n'); List <string> result = new List <string>(roughLineCount); int line = 0; ReadLineState flags = ReadLineState.NONE; result.Add(""); foreach (char ch in Code) { if (ch == '\n') { flags = ReadLineState.NONE; if (result[line].Trim().Length > 0) { line++; result.Add(""); } continue; } if (flags == ReadLineState.ESCAPE) { result[line] += ch; flags = ReadLineState.NONE; continue; } else if (flags == ReadLineState.COMMENT) { continue; } switch (ch) { case '\\': flags = ReadLineState.ESCAPE; break; case ';': flags = ReadLineState.COMMENT; break; case '\r': break; case '\t': result[line] += ' '; break; default: result[line] += ch; break; } } return(result.ToArray()); }
public void ReadLoop(object stateObject) { ReadLineState state = (ReadLineState)stateObject; while (true) { state.GetInputEvent.WaitOne(); state.Input = Console.ReadLine(); state.GotInputEvent.Set(); } }