2011-08-30 117 views
1

这的人难住我了。所以我正在用C#编写一个程序来为Terraria服务器提供一个GUI前端。我尝试了几种方法,但我总是遇到同样的问题。 首先我使用System.Diagnostic开始该过程。 后来我尝试了好几种东西,比如异步读取使用BeginOutputReadLine控制台输出或创建一个后台工作执行如下:C#与阅读标准输出问题

 while (!terrariaProcess.StandardOutput.EndOfStream) 
     { 
      consoleOutput.Items.Add(terrariaProcess.StandardOutput.ReadLine()); 
     } 

但产量永远是混乱的。 例 它应该是:(它的外观,如果我使用CMD来执行它)

Terraria Server v1.0.6.1 
Choose World: 
1  Test 
n  New World 
d <number> Delete World 
>>n 
Choose size: 
1  Small 
2  Medium 
3  Large 
>>3 
Enter World Name: 
>>Hello World 

但是我的程序把它读成:

Terraria Server v1.0.6.1 
1  Test 
n  New World 
d <number> Delete World 
>>n 
Choose World: Terraria Server v1.0.6.1 
1  Small 
2  Medium 
3  Large 
>>3 
Choose size: Terraria Server v1.0.6.1 
>>Hello World 

它这样做是不管我用什么方法。有人可以帮忙吗?我是一个白痴(再次)?

编辑: 乔恩的要求我做了个小型控制台应用程序,试图做同样的事情。我在循环中检查控制台输入时遇到了一些麻烦,所以我只能测试到第一个提示符,但它似乎仍然被破坏。 我的代码:

Process terrariaProcess; 
    terrariaProcess = new Process(); 
    terrariaProcess.StartInfo.FileName = "TerrariaServer.exe"; 
    terrariaProcess.StartInfo.UseShellExecute = false; 
    terrariaProcess.StartInfo.RedirectStandardInput = true; 
    terrariaProcess.StartInfo.RedirectStandardOutput = true; 
    terrariaProcess.StartInfo.CreateNoWindow = true; 
    terrariaProcess.Start(); 
    while (!terrariaProcess.StandardOutput.EndOfStream) 
    { 
     Console.WriteLine(terrariaProcess.StandardOutput.ReadLine()); 
    } 

结果输出:

Terraria Server v1.0.6.1 
1  Test 
n  New World 
d <number> Delete World 
+0

是否有可能,问题是你如何显示输出? –

+0

我对此表示怀疑。我得到的每一行输出都添加到ListBox中。我会尝试将它们添加到一个字符串,虽然挂在秒 –

+0

顺便说一句,你花你的整个人生徘徊在你的键盘看着这个网站?因为你总是回答我的问题。 (它总是我是一个白痴) –

回答

0

尝试调用函数Console.flush();

+0

修改原来的职位,我没有访问见方的陆族Server的源代码。 –