이상한 문제가 있습니다. Visual Studio 2012/13을 사용하여 C#에서 Windows 양식 응용 프로그램을 작성했습니다. 내 응용 프로그램은 콘솔 응용 프로그램을 열고 실행되는 동안 표준 및 오류 출력을 캡처 한 다음 해당 출력을 Windows 양식의 텍스트 상자에 인쇄합니다. 이전에는 잘 작동했지만, 실수로이 기능이 작동하지 않게 된 원인이 변경되었을 것입니다.RedirectStandardOutput이 콘솔 프로세스가 끝날 때까지 실행되지 않습니다.
이제 콘솔 앱이 끝나고 종료 된 후 콘솔 앱의 출력 만 Windows 형태로 출력됩니다. 나는 지금 당장 머리를 부딪 히고있다. Visual Studio 2012에서 2013로 업그레이드했지만 프로젝트가 변경되거나 업그레이드되지 않았으므로 그 원인을 알 수 없습니다. 어떤 도움을 주시면 감사하겠습니다!
나는 그것이 인쇄로 출력을 캡처하도록 설정 적절한 코드를 생각(이 어쨌든 일을하는 데 사용) :
transporterProcess.StartInfo.ErrorDialog = true;
transporterProcess.StartInfo.UseShellExecute = false;
transporterProcess.StartInfo.CreateNoWindow = true;
transporterProcess.StartInfo.RedirectStandardOutput = true;
transporterProcess.StartInfo.RedirectStandardError = true;
transporterProcess.EnableRaisingEvents = true;
transporterProcess.OutputDataReceived += new DataReceivedEventHandler(printOutput);
transporterProcess.ErrorDataReceived += new DataReceivedEventHandler(printError);
transporterProcess.Exited += new EventHandler(process_Exited);
...
transporterProcess.Start();
transporterProcess.BeginOutputReadLine();
transporterProcess.BeginErrorReadLine();
private void printOutput(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
{ // output is a global StringBuilder object
output.Clear();
output.Append(outLine.Data + Environment.NewLine);
string update = output.ToString();
updateTextfromStandardOutput = new Thread(new ThreadStart(() => this.ThreadProcSafeStandardOutput(update)));
this.updateTextfromStandardOutput.Start();
}
}
private void printError(object sendingProcess,
DataReceivedEventArgs outLine)
{
// pretty much identical to the above code
}
private void ThreadProcSafeStandardOutput(string update)
{
this.setText(update);
}
private void ThreadProcSafeStandardError(string update)
{
this.setText(update);
}
private void setText(string text)
{
if (tbxOutput.InvokeRequired || listViewFilesToTransfer.InvokeRequired)
{
setTextCallback d = new setTextCallback(setText);
tbxOutput.Invoke(d, new object[] { text });
}
else
{
...
사전에 감사!
'ProcessStartInfo' 객체에서'UseShellExecute = false','RedirectStandardOuput = true' 및'RedirectStandardError = true'를 설정 했습니까? –
이것은 전적으로 정상입니다. 리디렉션 된 프로그램은 출력을 내부 버퍼에 씁니다. 버퍼에 용량을 채우기에 충분한 텍스트를 쓰지 않고 버퍼 자체를 플러시하지 않으면 프로그램 종료시 버퍼 *가 플러시 될 때까지 아무 것도 볼 수 없습니다. 프로그램의 소스 코드가없는 경우에는 아무 것도 할 수 없습니다. –
@RichardDeeming 죄송합니다. 제 질문에서 빠뜨린 코드를 추가했습니다. –