troubleshooting Question

help needed running a command line program from

Avatar of andieje
andieje asked on
Visual Basic.NET
11 Comments1 Solution332 ViewsLast Modified:

I have some code for running a command line program from within This is shown below

I have added an event handler to handle the exit event of the program. Once the process exits and the event handler is called, where will program execution continue? Will it return to the main method? I am running the program now but the program takes so long so run its not easy to test things with.

Also my program is still running while the process is running. What is keeping the program running? Is it because the call outputstream.readToEnd does not complete until the process has finished so my program is waiting at this line?

what will happen to these function calls
out = outputStream.ReadToEnd()
err = errorStream.ReadToEnd
if the process exits because of an error? Will they run ok and still get any available output and error data? If not, how do i make sure that they do? Should i move these function calls to the Exit event handler.

Basically the order of  handling the exit event and then reading the output streams seems wrong to me. How should i do it properly?

Also, how would i echo the output of the process to the console window so i can see the output as the program is running?

Thanks very much. Here is the code


    Sub Main()


        Dim p As Process = New Process()
        Dim out As String
        Dim err As String

        Dim si As New ProcessStartInfo

        si.FileName = "C:\xmltv\xmltv-0.5.44-win32\xmltv-0.5.44-win32\xmltv.exe"
        si.Arguments = "tv_grab_uk_rt --output FILE"
        si.RedirectStandardError = True
        si.RedirectStandardOutput = True
        si.UseShellExecute = False
        si.CreateNoWindow = True
        p.StartInfo = si
        p.EnableRaisingEvents = True

        ' add an Exited event handler
        AddHandler p.Exited, AddressOf ProcessExited


        Dim outputStream As System.IO.StreamReader = p.StandardOutput
        Dim errorStream As System.IO.StreamReader = p.StandardError

        out = outputStream.ReadToEnd()
        err = errorStream.ReadToEnd



    End Sub

    ' event handler
    Friend Sub ProcessExited(ByVal sender As Object, _
       ByVal e As System.EventArgs)
        Dim myProcess As Process = DirectCast( _
           sender, Process)
        Console.WriteLine("The process exited, raising " & _
           "the Exited event at: " & myProcess.ExitTime & _
           "." & System.Environment.NewLine & _
           "Exit Code: " & myProcess.ExitCode)

    End Sub

Join our community to see this answer!
Unlock 1 Answer and 11 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros