기존 콘솔 응용 프로그램에 GUI를 제공하는 작은 포장 응용 프로그램이 있습니다. ProcessStartInfo
및 Process
클래스를 사용하여 .exe에 바인딩 한 다음 BeginErrorReadLine()
및 BeginOutputReadLine()
을 사용하여 메시지를 새 GUI로 리디렉션합니다. 콘솔이 Console.Write()
대신 Console.WriteLine()
을 호출 할 때를 제외하고는 모두 잘 작동합니다.이 경우 Write
에 전달 된 텍스트는 전혀 표시되지 않습니다. 나는 WriteLine
함수가 텍스트 뒤에 줄 바꿈을 삽입하고 Write
메서드가 그렇지 않기 때문에 문제가 있다고 생각합니다. 이 문제를 피할 방법이 있습니까? Write
은 원래 명령 줄 프로그램에서 Write
에서 WriteLine
으로 변경할 수 없으므로 입력을 요청하는 데 Write
이 사용됩니다.프로세스 래핑, 일부 출력이 표시되지 않습니다.
관련 코드 :
var startInfo = new ProcessStartInfo(ServerFile);
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
ServerProc = new Process();
ServerProc.StartInfo = startInfo;
ServerProc.EnableRaisingEvents = true;
ServerProc.ErrorDataReceived += new DataReceivedEventHandler(ServerProc_ErrorDataReceived);
ServerProc.OutputDataReceived += new DataReceivedEventHandler(ServerProc_OutputDataReceived);
private void ServerProc_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Dispatcher.Invoke(new Action(() =>
{
ConsoleTextBlock.Text += e.Data + "\r\n";
ConsoleScroll.ScrollToEnd();
}));
}
private void ServerProc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Dispatcher.Invoke(new Action(() =>
{
ConsoleTextBlock.Text += e.Data + "\r\n";
ConsoleScroll.ScrollToEnd();
}));
}
Sladkey 내가 비동기 및 동기 읽기를 사용할 때의 문제점은 필자가 직접 이벤트 기반이 아닌 쓰기 기능을 수동으로 호출해야한다는 것입니다. 이 올바른지? 가장 좋은 해결책은 타이머와 같을까요? – shmeeps
프로그램의 출력을 파싱하려면 파서를 작성해야합니다. 파서를 스레드로 작성하는 것이 가장 쉽습니다. 그렇지 않으면 상태 시스템이 필요합니다. 출력이 결정 론적이라면 타이머를 전혀 필요로하지 않습니다. 이 작업은 간단한 작업이 아니며 솔루션은 사용자의 필요와 응용 프로그램에 따라 다릅니다. 이 디자인은 구문 분석 스레드와의 협력이 필요하며 이벤트를 사용하는 것이 가장 쉽습니다. 그 외에도 많은 실용적인 접근 방식이 있습니다! –