Jump to content
Xtreme .Net Talk

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


Recommended Posts

Posted

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

problem.bmp

  • Leaders
Posted

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).

[sIGPIC]e[/sIGPIC]
Posted (edited)
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.

Edited by BrettW
Posted (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:

Edited by BrettW
  • Leaders
Posted
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.
[sIGPIC]e[/sIGPIC]
Posted (edited)

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:

Edited by BrettW
  • Administrators
Posted

The process object needs it's .EnableRaisingEvents property to be true before events will be raised. The following snippet should get you started.

 

 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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...