我知道这个问题主要是审美问题,但是这样的事情让我很恼火。我讨厌回答我自己的问题,但实际上我根据其他人的一些意见提出了一个相当不错的解决方案。我将附上一个代码示例,以防将来有人想做类似的事情。我知道可能有更好的方法来做到这一点,但这就是我所做的。
基本上,我没有在每个线程中使用 console.writeline,而是创建了一个新的子例程(TextOut),它背后有一些逻辑。我还更新了我的输入线程,将每个字符读入共享字符串,而不是使用 console.readline()。这是代码:
Private command As String = ""
Private Sub ConsoleInput()
Dim cki As ConsoleKeyInfo
cki = Console.ReadKey()
If cki.Key = ConsoleKey.Escape Then
command = "" 'Clear command
ElseIf cki.Key = ConsoleKey.Backspace Then
If Len(command) > 0 Then 'Make sure you don't go out of bounds
For i As Integer = 0 To Len(command)
Console.Write(" ") 'Clear output since new string will be shorter
Next
command = Left(command, Len(command) - 1) 'Shorten command by 1 char
End If
Console.CursorLeft = 0 'Set the cursor to the beginning of the line
Console.Write(command) 'Write the command to the screen
ElseIf cki.Key = ConsoleKey.Enter Then 'Command has been submitted, start checking
Console.CursorLeft = 0 'Set the cursor to the beginning of the line
For i As Integer = 0 To Len(command)
Console.Write(" ") 'Clear output from command (hides the executed command)
Next
Dim tempCMD As String = command
command = ""
'If/then statements for validating command goes here
command = "" 'Clear command to allow new input
Else
command += cki.KeyChar 'Add char to command string
Console.CursorLeft = 0 'Set the cursor to the beginning of the line
Console.Write(command) 'Write the command to the screen
End If
ConsoleInput() 'Loop for more input
End Sub
Sub TextOut(ByVal message As String)
If command <> "" Then
For i As Integer = 0 To Len(command)
Console.Write(" ") 'Clears output in case output is shorter than current command
Next
Console.CursorLeft = 0 'Sets cursor to beginning of row
End If
Console.WriteLine(message) 'Writes the current message to the screen
If message <> command Then
Console.Write(command) 'Writes the command back to the screen
End If
End Sub