0

Maybe I have the wrong approach here. I need to run a program to configure a piece of equipment. This has to run in the "background" and its basically a non batch program so it takes constant input in a loop from the user. I wrote the below code to launch the process and attempt to read/write from it but it doesn't work. I know the program is launching correctly, but I get no output until the program is killed. Am I doing something incorrectly?

Dim proc As Process
Dim prompt As String


Public Sub New()
    proc = New Process()

    AddHandler proc.OutputDataReceived, AddressOf CallbackDataReceived
    AddHandler proc.ErrorDataReceived, AddressOf CallbackErrorDataReceived

    prompt = ""

    Dim pi As ProcessStartInfo = New ProcessStartInfo()
    pi.UseShellExecute = False
    pi.FileName = "processname and location"
    pi.RedirectStandardInput = True
    pi.RedirectStandardOutput = True
    pi.RedirectStandardError = True
    pi.CreateNoWindow = True
    proc.StartInfo = pi
    proc.EnableRaisingEvents = True
    StartProcess()

    proc.BeginOutputReadLine()
End Sub

Public Sub StartProcess()
    proc.Start()
End Sub

Private Sub CallbackDataReceived(sender As Object, args As System.Diagnostics.DataReceivedEventArgs)
    If Not String.IsNullOrEmpty(args.Data) Then
        Console.Write(args.Data)
    End If
End Sub

Private Sub CallbackErrorDataReceived(sender As Object, args As System.Diagnostics.DataReceivedEventArgs)
    If Not String.IsNullOrEmpty(args.Data) Then
        Console.Write(args.Data)
    End If
End Sub

1 Answers1

0

You try to print when the output is empty. Change this

Private Sub CallbackDataReceived(sender As Object, args As System.Diagnostics.DataReceivedEventArgs)
    If String.IsNullOrEmpty(args.Data) Then
        Console.Write(args.Data)
    End If
End Sub

To this

Private Sub CallbackDataReceived(sender As Object, args As System.Diagnostics.DataReceivedEventArgs)
    If Not String.IsNullOrEmpty(args.Data) Then
        Console.Write(args.Data)
    End If
End Sub

Try setting a breakpoint and stepping through, this will help a lot next time.

Also, the output is received after a new line. I would suggest you do WriteLine instead. I think you won't receive anything if the process doesn't send a newline character.

the_lotus
  • 12,668
  • 3
  • 36
  • 53
  • That wasn't the issue but rather more of a typo in my submission. My code did have the 'Not' originally. I did try sending new lines, but still no dice. The events do not even fire. – Daniel Moss Sep 02 '16 at 13:05
  • @DanielMoss I copy/pasted your code, put the Not and called IpConfig and it worked. If there was a typo, I'm wondering if we are debugging the same program at all. – the_lotus Sep 02 '16 at 13:17
  • A different problem. IPConfig is a batch command so it finishes executing. Imagine you are trying to use something like telnet to a server and want to continually send commands and receive input. Like the program has an input loop. – Daniel Moss Sep 02 '16 at 13:41
  • @DanielMoss It works if it's sending newline. You might want to use the StandardOutput.BaseStream.BeginRead check [this](http://stackoverflow.com/questions/4501511/c-sharp-realtime-console-output-redirection) and [this](http://stackoverflow.com/questions/8625443/reading-standard-output-from-a-command-line-process-without-newline). Sometime it can seem "locked" when it's waiting for into right away, check [this](http://stackoverflow.com/questions/6655613/why-does-standardoutput-read-block-when-startinfo-redirectstandardinput-is-set). – the_lotus Sep 02 '16 at 14:27