0

I am trying to display what is going on in Command Line Interface without opening up command line. I know Process class has that ability but I am having a hard time with StandardOutput.ReadOnly. I have the process set to one button and then after I click, I want it to show a status that command line is in fact connecting. Thoughts?

    Private Sub EstablishConnection_Click(sender As Object, e As EventArgs) Handles EstablishConnection.Click

    ' One file parameter to the executable
    Dim sourceName1 As String = strXFileName
    Dim sourceName2 As String = strYFileName

    ' New ProcessStartInfo created
    Dim p As New ProcessStartInfo

    ' Specify the location of the binary
    p.FileName = "C:\Software\John\Doe"

    ' Use these arguments for the process
    p.Arguments = $"-Application  -Connect  -Example -E"" {stXFileName} "" -S"" {strYFileName} """

    ' Use a hidden window
    p.WindowStyle = ProcessWindowStyle.Hidden
    p.UseShellExecute = False
    p.RedirectStandardOutput = True
    p.RedirectStandardError = True
    p.RedirectStandardInput = True

    ' Start the process
    Process.Start(p)
    'Dim output As String = p.StandardOutputEncoding

    'Open the Status Screen form once connection is established
    StatusScreen.Show()
    Me.Hide()

End Sub
Lord45
  • 65
  • 8
  • 1
    Since you're using a GUI and you have redirected `stdOut` and `stdErr`, you could use the event handlers to receive the output. Add `[Process].BeginOutputReadLine()` and `[Process].BeginErrorReadLine()` and add a handler to `OutputDataReceived` and `ErrorDataReceived`. More or less as shown here: [How do I get output from a command to appear in a control on a Form in real-time?](https://stackoverflow.com/a/51682585/7444103). You can set the `SynchronizingObject` property to the current Form instance to avoid Invoking the UI Thread. – Jimi Dec 17 '19 at 20:41
  • @Jimi Thank you for responding. I have tried to use `Process.BeginOutputReadLine()` and `ErrorReadLine()` before. However, I get an exception saying "InvalidOperationException" (Begins asynchronous operations on the redirected Process.StandardError stream of the application) - Reference to a non-shared member requires an object reference. I am pretty new to vb, not sure what that means. – Lord45 Dec 18 '19 at 14:33
  • I want to know if the command in the code works on the command prompt. If it works, you can provide the file you are using so that we can test it. – Jack J Jun Dec 20 '19 at 06:42

0 Answers0