2012-03-01 4 views
0

VB.NET의 명령 줄 응용 프로그램에서 출력을 리디렉션하려고하는데 어떤 이유로 출력을 리디렉션하지 못합니다. 내 코드는 다음과 같습니다.VB.NET 프로세스 리디렉션 출력이 작동하지 않습니다.

Dim myProcess As Process = New Process 
    myProcess.StartInfo.FileName = "g++" 
    myProcess.StartInfo.Arguments = CMDLineCommand 
    myProcess.StartInfo.UseShellExecute = False 
    myProcess.StartInfo.RedirectStandardOutput = True 
    myProcess.StartInfo.RedirectStandardError = True 
    myProcess.StartInfo.CreateNoWindow = True 
    myProcess.Start() 
    Dim output As String = myProcess.StandardOutput.ReadToEnd 
    myProcess.WaitForExit() 
    CMDLineOutputTextBox.Text = output 

왜 리디렉션되지 않는지 아는 사람 있습니까? 미리 감사드립니다!

편집 -Neil

: 이상한

 Dim myProcess As Process = New Process 
     myProcess.StartInfo.FileName = "g++" 
     myProcess.StartInfo.Arguments = CMDLineCommand 
     myProcess.StartInfo.UseShellExecute = False 
     myProcess.StartInfo.RedirectStandardOutput = True 
     myProcess.StartInfo.RedirectStandardError = True 
     myProcess.StartInfo.CreateNoWindow = True 
     myProcess.EnableRaisingEvents = True 
     AddHandler myProcess.OutputDataReceived, AddressOf GotData 
     myProcess.Start() 
     CMDLineOutputTextBox.Text = "" 
     myProcess.BeginOutputReadLine() 

나중에 ...

Private Sub GotData(sendingProcess As Object, outLine As DataReceivedEventArgs) 
     If Not String.IsNullOrEmpty(outLine.Data) Then 
      SetText(outLine.Data) 
     End If 
End Sub 

Delegate Sub SetTextCallback(value As String) 
Private Sub SetText(ByVal value As String) 
    If Me.CMDLineOutputTextBox.InvokeRequired Then 
     Dim d As New SetTextCallback(AddressOf SetText) 
     Me.Invoke(d, New Object() {value}) 
    Else 
     Me.CMDLineOutputTextBox.Text += value + Environment.NewLine 
    End If 
End Sub 

아무것도 : 여기 내 전체 코드는 경우에 내 구문 이상한 거기에 아무것도이야?

답변

0

최종 출력물을 읽는 라인을 때릴 때까지 제공되는 출력은 모두 출력됩니다. g ++을 사용하고 있기 때문에 항상 그렇지 않을 수도 있습니다. OutputDataReceived 이벤트를 사용하고 그 데이터를 캡처하는 것이 더 나을 것입니다.

Dim myProcess As Process = New Process 
myProcess.StartInfo.FileName = "ping" 
myProcess.StartInfo.Arguments = "www.google.com" 
myProcess.StartInfo.UseShellExecute = False 
myProcess.StartInfo.RedirectStandardOutput = True 
myProcess.StartInfo.RedirectStandardError = True 
myProcess.StartInfo.CreateNoWindow = True 
myProcess.EnableRaisingEvents = True 
AddHandler myProcess.OutputDataReceived, AddressOf GotData 
myProcess.Start() 
myProcess.BeginOutputReadLine() 

그런 다음 당신은 이벤트 다음과 같이 처리 :

Private Sub GotData(sendingProcess As Object, outLine As DataReceivedEventArgs) 
    If Not String.IsNullOrEmpty(outLine.Data) Then 
     SetText(outLine.Data) 
    End If 
End Sub 

Delegate Sub SetTextCallback(value As String) 
Private Sub SetText(ByVal value As String) 
    If Me.TextBox3.InvokeRequired Then 
     Dim d As New SetTextCallback(AddressOf SetText) 
     Me.Invoke(d, New Object() {value}) 
    Else 
     Me.TextBox3.Text += value + Environment.NewLine 
    End If 
End Sub 
+0

위의 코드를 모두 추가했습니다 ... 어떤 이유로 CMDLineOutputTextBox 텍스트 상자에 명령 줄 출력이 표시되지 않습니다. P – neilf

+0

SetText 메서드에 중단 점을 설정하고 거기에 도착하는지 확인하십시오. 텍스트 상자가 여러 줄로 설정되어 있습니까? –

+0

예, 텍스트 상자는 여러 줄입니다. – neilf

0

내가 발견 한 waitforExit는 invoke.required 검사 중 코드 잠금을 할 것으로 보인다. 내가 대기 포기를 꺼내면 작동한다.

+1

waitForExit 실제로 기다리는 것을 의미합니까? :) – eckes

관련 문제