0
我目前正在寫的是被用作第二控制檯程序接口的程序,所以它應該閱讀程序的輸出,把它並在需要時發回的命令。C#/單聲道 - 讀輸出從一個控制檯應用程序
當我測試在Visual Studio中的Windows機器上的代碼,一切工作正常。但是當我在Ubuntu機器上用Mono(xbuild)編譯它時,我的程序無法讀取控制檯程序的輸出(並且我沒有收到任何異常或任何東西)
我的相關代碼是以下。在看到其他人在做什麼之後,我也試着用/bin/bash -c '/path/to/console_program'
和ProcessStartInfo
的論點來運行控制檯程序,但它給了我同樣的沉默結果。
private static ProcessStartInfo startInfo;
private static Process process;
private static Thread listenThread;
private delegate void Receive(string message);
private static event Receive OnReceive;
private static StreamWriter writer;
private static StreamReader reader;
private static StreamReader errorReader;
public static void Start(bool isWindows)
{
if(isWindows)
startInfo = new ProcessStartInfo("console_program.exe", "");
else
startInfo = new ProcessStartInfo("/path/to/console_program", "");
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.ErrorDialog = false;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
process = new Process();
process.StartInfo = startInfo;
bool processStarted = process.Start();
Console.WriteLine("[LOG] Engine started: " + processStarted.ToString());
writer = process.StandardInput;
reader = process.StandardOutput;
errorReader = process.StandardError;
OnReceive += new Receive(Engine_OnReceive);
listenThread = new Thread(new ThreadStart(Listen));
listenThread.Start();
}
private static void Engine_OnReceive(string message)
{
Console.WriteLine(message);
}
private static void Listen()
{
while (process.Responding)
{
string message = reader.ReadLine();
if (message != null)
{
OnReceive(message);
}
}
}
看到在那裏我應該修復它在Linux端工作的任何明顯的錯誤?
謝謝,這解決了Linux端的問題。 –