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( 「測試」)工作,但可能是有什麼區別標準輸出和標準錯誤之間?
沖水沒有幫助.. –
這是值得一試。這實際上是Python代碼可能存在的唯一潛在問題,所以我認爲解決方案將會在C#代碼中的某處。我絕對不能幫你 –