process.waitforexit not letting form load properly until it has exited

BrettW

Freshman
Joined
Nov 12, 2009
Messages
35
Hi.
I am making an app that runs a program just after a form loads. I want it to wait for the process to exit but when i use 'Process.WaitForExit' it does not let the form load properly. :mad: Any other ways to wait without suspending the form loading?
I have attached screen shot.

I am very appreciative of any help. :(:(:(:(
 

Attachments

Where exactly are you calling WaitForExit? The consructor? The OnLoad method? The Load event?

Also, regardless of where you call the method from, if you are doing it on the main thread it is going to freeze the UI. That means the application can't respond to user input, Windows events, or draw itself (which can lead to artifacts like those in your screenshot).
 
The .WaitForExit will just pause your application until the launched process has exited, you might find it more useful to handle it's .Exited event instead to be notified when the process has exited.
 
Where exactly are you calling WaitForExit? The consructor? The OnLoad method? The Load event?

Also, regardless of where you call the method from, if you are doing it on the main thread it is going to freeze the UI. That means the application can't respond to user input, Windows events, or draw itself (which can lead to artifacts like those in your screenshot).
I call it about 10 lines after I do form.show. Surely it should have loaded by then??
And by the way, how do you run it in a new thread as that might do the trick.
 
Last edited:
The .WaitForExit will just pause your application until the launched process has exited, you might find it more useful to handle it's .Exited event instead to be notified when the process has exited.
But if I do that how will I know when it has exited unless I do:
do until process.hasexited
Loop
'Rest of code here

But then the loop freezes the form as well?!!?
Any ideas? :confused:
 
Last edited:
You need to add an event handler to the Process object's Exited event. Use AddHandler or declare the variable at class-level as WithEvents and add a handler.
 
Hi.

The Process_Exited Sub does not seem to be working???

Here is the code:
Sub Process
Process1.StartInfo.FileName = "cmd.exe"
Process1.Start()
End Sub

Private Sub Process1_Exited(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Process1.Exited
MsgBox("CMD has closed")
End Sub

??!!?!??!??!!!??!!?!?!??!?!?! :confused::confused::confused::confused::confused:
 
Last edited:
The process object needs it's .EnableRaisingEvents property to be true before events will be raised. The following snippet should get you started.

Visual Basic:
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ps As New ProcessStartInfo
        ps.FileName = "cmd.exe"
        Dim p As New Process()
        p.EnableRaisingEvents = True    'required if using events
        p.StartInfo = ps
        AddHandler p.Exited, AddressOf Process1_Exited
        p.Start()

    End Sub

    Private Sub Process1_Exited(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MessageBox.Show("CMD has closed")
    End Sub
 
Back
Top