2012-09-19 20 views
0

我有用C#WPF編寫的GUI控制器,它使用args運行python腳本。在這個python腳本中有一些打印和寫入文件。我將stdout和stderr輸出重定向到TextBlock。但是這些輸出中沒有一個不起作用。有任何想法嗎?從另一個進程啓動時的Python輸出

C#亞軍:

... 
pr.StartInfo.FileName = pythonPath; 
pr.StartInfo.Arguments = scriptPath + " " + args; 
pr.StartInfo.UseShellExecute = false; 
pr.StartInfo.RedirectStandardOutput = true; 
pr.StartInfo.RedirectStandardError = true; 
pr.StartInfo.CreateNoWindow = true; 
pr.ErrorDataReceived += new DataReceivedEventHandler(sortErrorHandler); 
pr.OutputDataReceived += new DataReceivedEventHandler(sortOutputHandler); 
pr.EnableRaisingEvents = true; 
//pr.Exited += new EventHandler(whenExitProcess); 
TB.Text = ""; 
pr.Start(); 
pr.BeginOutputReadLine(); 
pr.BeginErrorReadLine();     
} 
e.Handled = true; 
} 



private void sortErrorHandler(object sendingProcess, DataReceivedEventArgs outLine) 
     { 
      Dispatcher.BeginInvoke(new MethodInvoker(delegate 
      { 
       if (!String.IsNullOrEmpty(outLine.Data)) 
       { 
        TB.Text += outLine.Data + Environment.NewLine; 

       } 
      })); 

    } 
    private void sortOutputHandler(object sendingProcess, DataReceivedEventArgs outLine) 
    {    
     Dispatcher.BeginInvoke(new MethodInvoker(delegate 
     { 
      if (!String.IsNullOrEmpty(outLine.Data)) 
      {     
       TB.Text += outLine.Data + Environment.NewLine;     
      } 
     })); 

    } 

的Python:

print "Hello" 
file.open("test", "a") 
file.write("Hello again") 
file.close() 
... remaining code 

好吧,sys.stderr.write( 「測試」)工作,但可能是有什麼區別標準輸出和標準錯誤之間?

回答

0

如果你只是沒有收到任何數據,你可能需要在你的python腳本中調用sys.stdout.flush()(和/或sys.stderr.flush())。

我不清楚你是否說寫入文件也有問題。

+0

沖水沒有幫助.. –

+0

這是值得一試。這實際上是Python代碼可能存在的唯一潛在問題,所以我認爲解決方案將會在C#代碼中的某處。我絕對不能幫你 –

相關問題