user4110222
user4110222

Reputation:

VB.net Redirecting Real Time process output

I have a problem with redirect real time process output, I'm running a java .jar file from vb.net process, and i want to redirect it. I created a console form, with richtextbox. The arguments are long, so I'm not write there. The code (it isn't redirecting anyithing) :

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.

Thx any ideas, and helpful answers!

Upvotes: 2

Views: 5198

Answers (2)

user4110222
user4110222

Reputation:

The problem was solved, the answer:

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

Upvotes: 4

user3453226
user3453226

Reputation:

You forgot to start the process.

p.Start()

Upvotes: 1

Related Questions