VB.net重定向实时进程输出

时间:2014-10-31 14:47:33

标签: vb.net process redirectstandardoutput

我有重定向实时流程输出的问题,我正在运行 java .jar文件 vb.net进程,我想重定向它。我用richtextbox创建了一个控制台表单。 争论很长,所以我不是在那里写的。代码(它没有重定向任何方式):

console.show() 'Console is a console window...
dim p as new process
p.startinfo.filename = java '(java as string: C:\Program Files\java....)
p.startinfo.argument = "-Xms2048M -Xmx4096M...."
p.startinfo.redirectstandardoutput = true
'what is comme there?
console.richtextbox1.text = 'and here is a redirected output, but i want redirect real time.

任何想法和有用的答案!

2 个答案:

答案 0 :(得分:4)

问题解决了,答案是:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

dim p as process()
p.startinfo.filename = "xyz"
p.startinfo.arguments = "...."
p.startinfo.workingdirectory = "some path"
p.startinfo.redirectstandarderror = true
p.startinfo.redirectstandardoutput = true
p.enableraisingevents = True
Application.DoEvents()
AddHandler proc.ErrorDataReceived, AddressOf proc_OutputDataReceived
AddHandler proc.OutputDataReceived, AddressOf proc_OutputDataReceived
p.start()
proc.BeginErrorReadLine()
proc.BeginOutputReadLine()

End Sub

Delegate Sub UpdateTextBoxDelg(text As String)
    Public myDelegate As UpdateTextBoxDelg = New UpdateTextBoxDelg(AddressOf UpdateTextBox)
    Public Sub UpdateTextBox(text As String)
        Console.RichTextBox1.Text += text & Environment.NewLine
        Console.RichTextBox1.SelectionStart = Console.RichTextBox1.Text.Length
        Console.RichTextBox1.ScrollToCaret()
    End Sub

    Public Sub proc_OutputDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
        If Me.InvokeRequired = True Then
            Me.Invoke(myDelegate, e.Data)
        Else
            UpdateTextBox(e.Data)
        End If
    End Sub

答案 1 :(得分:1)

你忘了开始这个过程。

p.Start()