내 WinForm 응용 프로그램에서 콘솔 응용 프로그램의 출력을 실시간으로 (cmd.exe를 통해 실행하는 것과 동일하게) 얻고 싶습니다. UI 작업이 아닌 스레드에서 수행하는 모든 작업 (BackgroundWorker의 메서드 bwRezerve_DoWork 사용). AddTextToTextbox
Invoke를 사용하여 UI를 업데이트하십시오.실시간으로 다른 응용 프로그램 표준 출력을 받으십시오.
하지만 지금은 응용 프로그램이 종료 될 때만 출력을받습니다. 많은 질문을 여기에서 읽고 다른 사이트에서 비슷한 질문을 Capture output of process synchronously (i.e. "when it happens") 읽고 있지만 여전히 해결책을 찾을 수 없습니다. 여기 코드 : 또한
private void bwRezerve_DoWork(object sender, DoWorkEventArgs e)
{
proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = Application.StartupPath + Path.DirectorySeparatorChar + "7z.exe",
Arguments = e.Argument,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
}
};
proc.EnableRaisingEvents = true;
proc.OutputDataReceived += (who, what) => AddTextToTextbox(what.Data);
proc.ErrorDataReceived += (who, what) => AddTextToTextbox(what.Data);
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
//same result with next line commented
proc.WaitForExit(5 * 60 * 1000);
}
내가 대신 OutputDataReceived
의이 시도했지만 결과는이 코드
private void bwRezerve_DoWork(object sender, DoWorkEventArgs e)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = Application.StartupPath + Path.DirectorySeparatorChar + "7z.exe";
psi.Arguments = e.Argument;
psi.UseShellExecute = false;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;
Process proc = Process.Start(psi);
proc.WaitForExit();
while (!proc.StandardOutput.EndOfStream)
{
string line = proc.StandardOutput.ReadLine();
AddTextToTextbox(line);
}
}
이 경우 입력을 전혀받지 못했습니다. – Varro
업데이트 된 코드 시도 – Mostafiz
ReadLine()과 ReadToEnd()를 모두 사용했습니다. 결과는 동일합니다. – Varro