Implementation umgebaut, sodass sie besser der Aufgabenstellung entspricht
This commit is contained in:
@@ -6,7 +6,7 @@ public class Terminal
|
||||
{
|
||||
private Object TerminalLock = new Object ();
|
||||
private Object ReadLineLock = new Object ();
|
||||
private AutoResetEvent ResetEvent = new AutoResetEvent(false);
|
||||
private AutoResetEvent ResetEvent = new AutoResetEvent (false);
|
||||
private String InputBuffer = "";
|
||||
private bool AcceptInput = false;
|
||||
private int OutLinePos;
|
||||
@@ -14,7 +14,7 @@ public class Terminal
|
||||
public Terminal ()
|
||||
{
|
||||
OutLinePos = Console.CursorTop;
|
||||
Thread catchInputThread = new Thread(new ThreadStart(this.CatchInput));
|
||||
Thread catchInputThread = new Thread (new ThreadStart (this.CatchInput));
|
||||
catchInputThread.Start ();
|
||||
}
|
||||
|
||||
@@ -23,29 +23,23 @@ public class Terminal
|
||||
while (true) {
|
||||
ConsoleKeyInfo key = Console.ReadKey (false);
|
||||
lock (TerminalLock) {
|
||||
if (!AcceptInput)
|
||||
{
|
||||
if (!AcceptInput) {
|
||||
Console.CursorLeft--;
|
||||
Console.Write(" ");
|
||||
Console.Write (" ");
|
||||
Console.CursorLeft--;
|
||||
continue;
|
||||
}
|
||||
if (key.Key == ConsoleKey.Backspace)
|
||||
{
|
||||
if (key.Key == ConsoleKey.Backspace) {
|
||||
if (InputBuffer.Length == 0)
|
||||
continue;
|
||||
InputBuffer = InputBuffer.Substring(0, InputBuffer.Length - 1);
|
||||
InputBuffer = InputBuffer.Substring (0, InputBuffer.Length - 1);
|
||||
ClearInputLine ();
|
||||
Console.Write (InputBuffer);
|
||||
}
|
||||
else if (key.Key == ConsoleKey.Enter)
|
||||
{
|
||||
} else if (key.Key == ConsoleKey.Enter) {
|
||||
AcceptInput = false;
|
||||
ResetEvent.Set ();
|
||||
OutLinePos = Console.CursorTop;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
InputBuffer += key.KeyChar;
|
||||
ClearInputLine ();
|
||||
Console.Write (InputBuffer);
|
||||
@@ -57,7 +51,7 @@ public class Terminal
|
||||
public void PrintLine (String s)
|
||||
{
|
||||
lock (TerminalLock) {
|
||||
ClearInputLine();
|
||||
ClearInputLine ();
|
||||
Console.WriteLine (s);
|
||||
OutLinePos = Console.CursorTop;
|
||||
Console.Write (InputBuffer);
|
||||
@@ -67,12 +61,21 @@ public class Terminal
|
||||
public String ReadLine ()
|
||||
{
|
||||
lock (ReadLineLock) {
|
||||
String result;
|
||||
AcceptInput = true;
|
||||
ResetEvent.WaitOne();
|
||||
result = InputBuffer;
|
||||
InputBuffer = "";
|
||||
return result;
|
||||
try {
|
||||
String result;
|
||||
AcceptInput = true;
|
||||
ResetEvent.WaitOne ();
|
||||
result = InputBuffer;
|
||||
InputBuffer = "";
|
||||
return result;
|
||||
} catch (ThreadAbortException e) {
|
||||
lock (TerminalLock) {
|
||||
AcceptInput = false;
|
||||
InputBuffer = "";
|
||||
ClearInputLine ();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user