BrettW Posted November 12, 2009 Posted November 12, 2009 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 Quote
Leaders snarfblam Posted November 12, 2009 Leaders Posted November 12, 2009 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). Quote [sIGPIC]e[/sIGPIC]
Administrators PlausiblyDamp Posted November 12, 2009 Administrators Posted November 12, 2009 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
BrettW Posted November 13, 2009 Author Posted November 13, 2009 (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 November 13, 2009 by BrettW Quote
BrettW Posted November 13, 2009 Author Posted November 13, 2009 (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 November 13, 2009 by BrettW Quote
Administrators PlausiblyDamp Posted November 13, 2009 Administrators Posted November 13, 2009 The event will be raised when the process exits - you would simply need to put the appropriate code into the event handler. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders snarfblam Posted November 13, 2009 Leaders Posted November 13, 2009 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. Quote [sIGPIC]e[/sIGPIC]
BrettW Posted November 14, 2009 Author Posted November 14, 2009 Thanks I will try this.:):):):) Quote
BrettW Posted November 14, 2009 Author Posted November 14, 2009 (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 November 14, 2009 by BrettW Quote
Administrators PlausiblyDamp Posted November 14, 2009 Administrators Posted November 14, 2009 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 Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.