Hi all,
I want to redirect the console output in real time to a textbox. And if any standard error arrives or Process completed than i have to give one msgbox stating that process completed.Is there any method avalible.Presently I have this code.But it is not working properly

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.TextBox1.Text = String.Empty
        Dim clsProcess As New System.Diagnostics.Process()
        clsProcess.StartInfo.UseShellExecute = False
        clsProcess.StartInfo.RedirectStandardOutput = True
        clsProcess.StartInfo.RedirectStandardError = True
        clsProcess.StartInfo.FileName = "cmd dir/s"
        clsProcess.StartInfo.CreateNoWindow = True
        clsProcess.Start()
        While (clsProcess.HasExited = False)
            Dim sLine As String = clsProcess.StandardOutput.ReadLine
            If (Not String.IsNullOrEmpty(sLine)) Then
                Me.TextBox1.Text &= sLine & vbCrLf
            End If
            Application.DoEvents()
        End While
        Me.TextBox1.Text += "Completed"
    End Sub

Thanks in Advance
Dana

Dani AI

Generated

Good start from and sensible hint from about splitting executable and arguments. The core problem is the synchronous ReadLine loop and Application.DoEvents: that pattern can miss output, freeze the UI, or deadlock when both stdout and stderr are redirected. An event-driven approach (OutputDataReceived / ErrorDataReceived + Exited) keeps the UI responsive and guarantees real-time lines arrive in order.

The example below shows the pattern: redirect both streams, start async readers, update the TextBox via BeginInvoke (thread-safe), set a flag when any standard error arrives, and show one MessageBox on process exit that reflects whether errors were seen.

Private errorSeen As Boolean = False

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox1.Clear()
    errorSeen = False

    Dim p As New Process()
    With p.StartInfo
        .FileName = "cmd.exe"
        .Arguments = "/c dir /s"    ' executable and arguments kept separate
        .UseShellExecute = False
        .RedirectStandardOutput = True
        .RedirectStandardError = True
        .CreateNoWindow = True
    End With

    p.EnableRaisingEvents = True

    AddHandler p.OutputDataReceived, Sub(s, ev)
                                         If ev.Data IsNot Nothing Then AppendTextThreadSafe(ev.Data & vbCrLf)
                                     End Sub

    AddHandler p.ErrorDataReceived, Sub(s, ev)
                                        If ev.Data IsNot Nothing Then
                                            errorSeen = True
                                            AppendTextThreadSafe("ERROR: " & ev.Data & vbCrLf)
                                        End If
                                    End Sub

    AddHandler p.Exited, Sub()
                             Me.BeginInvoke(Sub()
                                                MessageBox.Show(If(errorSeen, "Completed (errors were produced).", "Completed successfully."))
                                            End Sub)
                         End Sub

    p.Start()
    p.BeginOutputReadLine()
    p.BeginErrorReadLine()
End Sub

Private Sub AppendTextThreadSafe(text As String)
    If TextBox1.InvokeRequired Then TextBox1.BeginInvoke(Sub() TextBox1.AppendText(text)) Else TextBox1.AppendText(text)
End Sub

Notes and troubleshooting: avoid synchronous ReadToEnd when both streams are redirected (risk of deadlock). Keep UseShellExecute = False and use /c with cmd.exe to run a command and exit. Updating the UI must be marshalled with Invoke/BeginInvoke. For immediate popups on the first error, show a MessageBox from the ErrorDataReceived handler via BeginInvoke instead of waiting for Exited.

Hi

Your code is almost there. All that is required is to break apart the filename and add arguments. See below

p

Dim clsProcess As New System.Diagnostics.Process()
clsProcess.StartInfo.UseShellExecute = False
clsProcess.StartInfo.RedirectStandardOutput = True
clsProcess.StartInfo.RedirectStandardError = True
clsProcess.StartInfo.FileName = "cmd.exe"
clsProcess.StartInfo.Arguments = "dir /s"
clsProcess.StartInfo.CreateNoWindow = True
clsProcess.Start()
While (clsProcess.HasExited = False)
Dim sLine As String = clsProcess.StandardOutput.ReadLine
If (Not String.IsNullOrEmpty(sLine)) Then

End If
Me.TextBox1.Text &= sLine & vbCrLf

Application.DoEvents()
End While
Me.TextBox1.Text += "Completed"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.