2011-05-06 57 views
2

我正在讀取使用進程啓動並讀取stdError,stdOutout並使用stdInput發送命令的java應用程序的輸出。以下是相關代碼:標準輸出暫停循環結束流C#

 int mem = Properties.Settings.Default.mem_max; 
     string locale = Properties.Settings.Default.location; 
     Process bukkit_jva = new Process(); 
     bukkit_jva.StartInfo.FileName = "java"; 
     //bukkit_jva.StartInfo.Arguments = "-Xmx" + mem + "M -Xms" + mem + "M -jar " + locale + "bukkit.jar"; 
     bukkit_jva.StartInfo.Arguments = "-Xmx512M -Xms512M -jar C:\\bukkit\\bukkit.jar"; 
     bukkit_jva.StartInfo.UseShellExecute = false; 
     bukkit_jva.StartInfo.CreateNoWindow = true; 
     bukkit_jva.StartInfo.RedirectStandardError = true; 
     bukkit_jva.StartInfo.RedirectStandardOutput = true; 
     bukkit_jva.StartInfo.RedirectStandardInput = true; 
     bukkit_jva.Start(); 
     //start reading output 
     SetText(bukkit_jva.StandardOutput.ReadLine()); 
     SetText(bukkit_jva.StandardOutput.ReadLine()); 
     SetText(bukkit_jva.StandardOutput.ReadLine()); 
     SetText(bukkit_jva.StandardOutput.ReadLine()); 
     StreamReader err = bukkit_jva.StandardError; 
     StreamReader output = bukkit_jva.StandardOutput; 
     StreamWriter writer = bukkit_jva.StandardInput; 
     SetText(err.Peek().ToString()); 
     while (false == false) 
     { 
      if (vars.input != null) 
      { 
       writer.WriteLine(vars.input); 
       vars.input = null; 
      } 
      SetText(output.ReadLine() + err.ReadLine()); 
     } 
    } 

SetText()將該行添加到行列表中。

我的問題是,即使沒有輸入,java應用程序有時也會返回一個字符串,所以我總是需要檢查一個新行。但是如果我需要發送一個命令,並且沒有新的輸出,它將不會發送。

我在readline上嘗試了不同的If語句,但它只會返回前幾行然後停止。

基本上它似乎暫停循環,如果沒有新行可以讀取。

我怎麼能不同地設置我的讀/寫循環或讓循環取消暫停?

感謝, 亞當

+0

你知道你可以用'while(true)'替換'while(false == false)',對吧? – Dani 2011-05-06 23:10:39

回答

0

試試這個:

static void Main(string[] args) 
{ 
    ProcessStartInfo psi = new ProcessStartInfo("echoApp.exe"); 
    psi.RedirectStandardInput = true; 
    psi.RedirectStandardOutput = true; 
    psi.RedirectStandardError = true; 
    psi.UseShellExecute = false; 

    Process echoApp = new Process(); 
    echoApp.ErrorDataReceived += new DataReceivedEventHandler(echoApp_ErrorDataReceived); 
    echoApp.OutputDataReceived += new DataReceivedEventHandler(echoApp_OutputDataReceived); 
    echoApp.StartInfo = psi; 
    echoApp.Start(); 
    echoApp.BeginOutputReadLine(); 
    echoApp.BeginErrorReadLine(); 
    echoApp.StandardInput.AutoFlush = true; 

    string str = ""; 
    while (str != "end") 
    { 
     str = Console.ReadLine(); 
     echoApp.StandardInput.WriteLine(str); 
    } 
    echoApp.CancelOutputRead(); 
    echoApp.CancelErrorRead(); 
    echoApp.Close(); 
} 

static void echoApp_OutputDataReceived(object sender, DataReceivedEventArgs e) 
{ 
    Console.WriteLine("stdout: {0}", e.Data); 
} 

static void echoApp_ErrorDataReceived(object sender, DataReceivedEventArgs e) 
{ 
    Console.WriteLine("stderr: {0}", e.Data); 
} 

和小echoApp ...

//echoApp 
static void Main(string[] args) 
{ 
    string str=""; 
    while (str != "end") 
    { 
     str = Console.ReadLine(); 
     Console.WriteLine(str); 
    } 
} 
+0

工程就像一個魅力! – 2011-05-07 00:03:23

0

請注意,如果您嘗試訪問CancelOutputRead後的輸出()和CancelErrorRead()您可能會發現偶爾會缺少一些文本。我發現只有在明確調用Close()後纔會發生刷新。處置(使用聲明)不起作用。

當調用命令處理器(CMD.EXE)時,很可能會出現此症狀,因爲它不會顯式刷新自身。所以要小心不要嘗試訪問輸出(從事件處理程序中寫入),除非您先明確調用Close()。

+0

感謝您的提示。 – 2012-06-07 15:12:27