Question Wait for shelled process to finish?

darxide

Member
Joined
Mar 30, 2012
Messages
9
Programming Experience
1-3
First, a bit of pseudo-ish code:

VB.NET:
    Loop Start

           //Some logic here. Removed for brevity

            Dim p As New ProcessStartInfo
            p.FileName = _NAME
            p.Arguments = _ARGS
            p.WindowStyle = ProcessWindowStyle.Hidden

            Process.Start(p)

    Loop End

So I'm using something like the above to execute an external command line program several hundred times. It works great, except that it bogs the entire system down while it's running. Switching window style to .Normal shows me that upwards of 30 or 40 different instances of the console are happening at once. I found some info on the WaitForExit method, but it's unavailable to console applications.

Is there a way to accomplish this in a console application? I don't mind the extra wait time it would incur just as long as the computer is still usable while it's going. Placing an arbitrary pause after each Process.Start(p) is less than ideal since each execution of the external process has different arguments and will take differing amounts of time to complete.

Thanks
 
Last edited:
I found some info on the WaitForExit method, but it's unavailable to console applications.
Are you sure about that? I haven't tested but I've never heard that. As long as Process.Start returns a Process object, as far as I'm aware, you should be able to call WaitForExit on that object. That's what's important. Sometimes Process.Start doesn't return a Process object, e.g. you pass a URL and an already-open browser displays it in a new tab. In that case there is no new process so no Process object is created.
 
That's what I've been reading and when I try doing p.WaitForExit I get the message: 'WaitForExit' is not a member of 'System.Diagnostics.ProcessStartInfo'.
 
I just tested and you are incorrect. I wrote this Console app:
Imports System.Threading

Module Module1

    Sub Main(args As String())
        Console.WriteLine("Start")

        If args.Length = 0 Then
            Thread.Sleep(5000)
        Else
            For i As Integer = 0 To 5
                Thread.Sleep(1000)
                Console.WriteLine(args(0))
            Next
        End If

        Console.WriteLine("End")
        Console.ReadLine()
    End Sub

End Module
and invoked it using this WinForms app:
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim psi As New ProcessStartInfo("C:\Users\jmcilhinney\AppData\Local\Temporary Projects\ConsoleTest\bin\Debug\ConsoleTest.exe", TextBox1.Text)
        Dim p = Process.Start(psi)

        p.WaitForExit()

        MessageBox.Show("Done")
    End Sub

End Class
and the message box did not appear until the Console window closed. I thought maybe you meant that the caller, rather than the callee, was a Console app so I tried this too:
Module Module1

    Sub Main()
        Dim psi As New ProcessStartInfo("C:\Users\jmcilhinney\AppData\Local\Temporary Projects\ConsoleTest\bin\Debug\ConsoleTest.exe", Console.ReadLine())
        Dim p = Process.Start(psi)

        p.WaitForExit()

        Console.WriteLine("Done")
        Console.ReadLine()
    End Sub

End Module
Once again, the message was not displayed in the caller until the callee closed. If you're seeing different then it's not simply because you're running a Console app.
 
That's what I've been reading and when I try doing p.WaitForExit I get the message: 'WaitForExit' is not a member of 'System.Diagnostics.ProcessStartInfo'.

The error message is quite correct. As I said in my first post, you call WaitForExit on the Process object returned by Process.Start, not on the ProcessStartInfo object.
 
Yes, this is because my original code should look like this instead:

VB.NET:
    Loop Start

           //Some logic here. Removed for brevity

            Dim p As New ProcessStartInfo
            dim myProcess as Process = Nothing
            p.FileName = _NAME
            p.Arguments = _ARGS
            p.WindowStyle = ProcessWindowStyle.Hidden

            myProcess = Process.Start(p)
            myProcess.WaitForExit

    Loop End

I had been using p.WaitForExit which did not work. It was as simple as that.
 
Back
Top