2010-05-20 13 views
-1

參考這些帖子:here1here2最後,我通過構建一個異步解決方案來解決我的問題,並且它工作得很好!但我面對這一個問題,現在我的代碼是這樣的:如何在異步過程中從shell中檢索完全結果?

class MyProcessStarter 
    { 
     private Process process; 
     private StreamWriter myStreamWriter; 
     private static StringBuilder shellOutput = null; 
     public String GetShellOutput { get { return shellOutput.ToString(); }} 

     public MyProcessStarter(){ 
      shellOutput = new StringBuilder(""); 
      process = new Process();    
      process.StartInfo.FileName = "sqlplus"; 
      process.StartInfo.UseShellExecute = false; 
      process.StartInfo.CreateNoWindow = true; 
      process.OutputDataReceived += new DataReceivedEventHandler(ShellOutputHandler); 

      process.StartInfo.RedirectStandardInput = true; 
      process.StartInfo.RedirectStandardOutput = true; 
      //process.StartInfo.RedirectStandardError = true; 
      process.Start(); 
      myStreamWriter = process.StandardInput; 
      process.BeginOutputReadLine(); 
     } 

     private static void ShellOutputHandler(object sendingProcess,DataReceivedEventArgs outLine) 
     { 
      if (!String.IsNullOrEmpty(outLine.Data)) 
       shellOutput.Append(Environment.NewLine + outLine.Data); 
     } 

     public void closeConnection() 
     { 
      myStreamWriter.Close(); 
      process.WaitForExit(); 
      process.Close(); 
     } 

     public void RunCommand(string arguments) 
     { 
      myStreamWriter.WriteLine(arguments); 
      myStreamWriter.Flush(); 
      process.WaitForExit(100); 
      Console.WriteLine(shellOutput); 
      Console.WriteLine("============="+Environment.NewLine); 
      process.WaitForExit(2000); 
      Console.WriteLine(shellOutput);    
     } 
    } 

和我的輸入是這樣的:

myProcesStarter.RunCommand("myusername/mypassword"); 
Console.writeline(myProcesStarter.GetShellOutput); 

,但看看我出去放:

SQL*Plus: Release 11.1.0.6.0 - Production on Thu May 20 11:57:38 2010 
Copyright (c) 1982, 2007, Oracle. All rights reserved. 
============= 


SQL*Plus: Release 11.1.0.6.0 - Production on Thu May 20 11:57:38 2010 
Copyright (c) 1982, 2007, Oracle. All rights reserved. 
Enter user-name: 
Connected to: 
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production 
With the Partitioning, OLAP, Data Mining and Real Application Testing options 

因爲你看運行的輸出功能在不同的時間是不一樣的所以現在你會做我一個發燒友,幫助我,我可以等待,直到所有的輸出完成其他意味着如何我可以定製我的過程要等到輸出完成ing?因爲我想編寫一個sqlcompiler,所以我需要shell的確切輸出。

plz幫助我soon.thanxxxxxxxxxxxx:X

回答

1

您應該使用-S參數上的SQL * Plus。據幫助下,將該參數置

設置靜音模式這抑制了 顯示SQL * Plus的旗幟, 提示和命令的呼應。

理想情況下,這會使輸出保持一致。