Results 1 to 6 of 6

Thread: Wait for shelled process to finish?

  1. #1
    darxide is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Mar 2012
    Posts
    9
    Reputation
    0

    Wait for shelled process to finish?

    First, a bit of pseudo-ish code:

    Code:
        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 by darxide; 07-30-2013 at 12:01 PM. Reason: Edit for clarity

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,767
    Reputation
    1702
    Quote Originally Posted by darxide View Post
    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.

  3. #3
    darxide is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Mar 2012
    Posts
    9
    Reputation
    0
    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'.

  4. #4
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,767
    Reputation
    1702
    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.

  5. #5
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,767
    Reputation
    1702
    Quote Originally Posted by darxide View Post
    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.

  6. #6
    darxide is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Mar 2012
    Posts
    9
    Reputation
    0
    Yes, this is because my original code should look like this instead:

    Code:
        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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •