BeginOutputReadLine not resuming reading lines after process restart

I have an application where I'd like to have the user start over and over a console application ans watch its output redirected in the GUI.
I have noticed that BeginOutputReadLine will correctly get the stdout redirected on the process 1st execution, but will resume reading lines after the process is restarted.

I have managed to reproduce on a small example which as a console application starts a built-in operating system app, which is:




Public Class Form1
    Private firsttime As Boolean
    Private Shared numOutputLines As Integer = 0
    Private filetostart As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        filetostart = "c:\windows\system32\ipconfig.exe"
        firsttime = True
    End Sub

    Private Sub calcola()
        If (My.Computer.FileSystem.FileExists(filetostart)) Then

            If (firsttime) Then
                Dim value As ProcessStartInfo
                value = New ProcessStartInfo
                value.FileName = filetostart
                value.CreateNoWindow = True
                value.RedirectStandardOutput = True
                value.WindowStyle = ProcessWindowStyle.Hidden
                value.UseShellExecute = False
                Process1.StartInfo = value
                Process1.EnableRaisingEvents = True
                value = Nothing
                firsttime = False
                AddHandler Process1.OutputDataReceived, AddressOf Process1_OutputDataReceived
                AddHandler Process1.Exited, AddressOf Process1_Exited
                AddHandler Process1.Disposed, AddressOf Process1_Disposed
            End If

            Process1.Start()
            numOutputLines = 0
            Me.TextBox1.Text = "Process started "
            Process1.BeginOutputReadLine()
        Else
            MsgBox("File """ & filetostart & """ does not exist !", MsgBoxStyle.Exclamation, "File not found")
        End If
    End Sub
    Private Sub fine()
        Process1.CancelOutputRead()
        Process1.Close()
    End Sub
    Private Sub Process1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Process1.Disposed
        Call fine()
    End Sub
    Private Sub Process1_Exited(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Process1.Exited
        Call fine()
    End Sub

    Private Sub Process1_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles Process1.OutputDataReceived
        If Not String.IsNullOrEmpty(e.Data) Then
            numOutputLines += 1
            Me.TextBox1.AppendText(Environment.NewLine + "[" _
                         + Format(numOutputLines, "0000") + "] - " _
                         + e.Data)
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        calcola()
    End Sub
End Class

 


So I am posting this here in case anybody has seen this before and before reporting it as a bug.

Paolo




Answer this question

BeginOutputReadLine not resuming reading lines after process restart

  • Greg3055

    I'm getting threading errors due to having different version of Visual studio.

    However, looking at the code, i think if you try this layout it should work ok.




            If (My.Computer.FileSystem.FileExists(filetostart)) Then
                Dim value As ProcessStartInfo = New ProcessStartInfo
                With value
                    .FileName = filetostart
                    .CreateNoWindow = True
                    .RedirectStandardOutput = True
                    .WindowStyle = ProcessWindowStyle.Hidden
                    .UseShellExecute = False
                End With

                With Process1
                    .StartInfo = value
                    .EnableRaisingEvents = True
                    If (firsttime) Then
                        firsttime = False
                        AddHandler .OutputDataReceived, AddressOf Process1_OutputDataReceived
                        AddHandler .Exited, AddressOf Process1_Exited
                        AddHandler .Disposed, AddressOf Process1_Disposed
                    End If
                    .Start()
                End With

                numOutputLines = 0
                Me.TextBox1.Text = "Process started "
                Process1.BeginOutputReadLine()
            Else
                MsgBox("File """ & filetostart & """ does not exist !", MsgBoxStyle.Exclamation, "File not found")
            End If

     


    Hope this helps,

    Dustin.


  • doubletree

    Of course I meant "I have noticed that BeginOutputReadLine will correctly get the stdout redirected on the process 1st execution, but will NOT resume reading lines after the process is restarted."


  • BeginOutputReadLine not resuming reading lines after process restart