dannyres Posted October 13, 2003 Posted October 13, 2003 Hi guys, im trying to make a program that will start a Process and then handle the Process's Exited event and do some code when the application is exited. I dont need to display a form for this so i decided i'd make a formless app. This is my code: Imports System.Diagnostics Public Class Test Shared WithEvents P As Process Shared args() As String Shared Sub Main() args = System.Environment.GetCommandLineArgs P = New Process P = Process.Start(args(1)) End Sub Private Shared Sub P_Exited(ByVal sender As Object, ByVal e As System.EventArgs) Handles P.Exited Process.Start(args(2)) End Sub End Class My problem is, the application exits itself as soon as the Main Sub is exited. Is there anyway to prevent this? or another way of doing my code alltogether? Thanks, Dan Quote
*Experts* Nerseus Posted October 13, 2003 *Experts* Posted October 13, 2003 You'll want to wait on the process to finish (I would assume). I can't recall offhand what it is though - does the Process.Start method have any extra params such as wait til exit? If not, you may have to find out how to wait on a handle until it closes (sounds familiar, like I've done that before?) - the handle being returned from the Start method I think. Sorry for the vaguness, I'll look into it more in the morning if you still need help. If you can't find a way to wait using .NET, here's a link to doing it using the API (non-.NET code, sorry): http://www.thescarms.com/VBasic/wait.asp -nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Mohsen Posted October 13, 2003 Posted October 13, 2003 Why don't u use threads? The code in the thread will continue after the Main is exited, so I think that would definetly solve your problem. try to do the following, Imports System.Diagnostics Public Class Test Shared WithEvents P As Process Shared args() As String private ProcessThread as Threading.Thread = New Threading.Thread(AddressOf StartProcess) Shared Sub Main() args = System.Environment.GetCommandLineArgs ProcessThread .Start() End Sub private sub StartProcess P = New Process P = Process.Start(args(1)) End sub Private Shared Sub P_Exited(ByVal sender As Object, ByVal e As System.EventArgs) Handles P.Exited Process.Start(args(2)) End Sub End Class That will work, please send me an email to mohsenfarran@yahoo.com , and keep me updated to the result code that I've supplied to you. Quote
dannyres Posted October 13, 2003 Author Posted October 13, 2003 Mohsen - I had a play around with the code you posted and I couldnt get it to work, the second application is never launched after the first is terminated. Anyone have any ideas? Dan Quote
Engine252 Posted October 13, 2003 Posted October 13, 2003 if i can asume you want to start applications with those args there are 2 ways to solve you problem 1. wait for the second process to end before ending your main application since the process is killed from the moment you close your app. 2. you could use the shell command (shell("application name")) in that case the main app can be closed before the end of the argument app. ;-) Quote
dannyres Posted October 14, 2003 Author Posted October 14, 2003 hmm Engine252 i dont really get what your saying there... any chance you could whip up an example for me? Dan Quote
Mohsen Posted October 14, 2003 Posted October 14, 2003 Try this, Imports System.Diagnostics Public Class Test Shared WithEvents P As Process Shared args() As String private ProcessThread as Threading.Thread = New Threading.Thread(AddressOf StartProcess) Shared Sub Main() args = System.Environment.GetCommandLineArgs P = New Process P = Process.Start(args(1)) End Sub Private Shared Sub P_Exited(ByVal sender As Object, ByVal e As System.EventArgs) Handles P.Exited ProcessThread .Start() End Sub private sub StartProcess Process.Start(args(2)) End sub End Class Dear Dan, thry that code, I think it's better from what I've sent you previously. The idea here is that on exiting the first application, a thread lunches the second one. And keep me updated. Moshen Quote
Mohsen Posted October 14, 2003 Posted October 14, 2003 Actually it should be, P = Process.Start(args(2)) not Process.Start(args(2)) Moshen Quote
dannyres Posted October 14, 2003 Author Posted October 14, 2003 Mohsen - that doesnt seem to work either :/ i think the problem is, is that the application is exited before P_Exited is even called. Dan Quote
dannyres Posted October 14, 2003 Author Posted October 14, 2003 Ok ive done a bit of looking around and i *think* the this might be usful though im not sure.. System.Threading.WaitHandle Anyone know how to use this? Dan Quote
*Gurus* divil Posted October 14, 2003 *Gurus* Posted October 14, 2003 This will do what you need. Friend Class Class1 Dim WithEvents p As Process Public Shared Sub Main() Dim c As New Class1 c.Start() End Sub Public Sub Start() Dim ps As New ProcessStartInfo("notepad.exe") p = Process.Start(ps) p.EnableRaisingEvents = True Application.Run() End Sub Private Sub p_Exited(ByVal sender As Object, ByVal e As System.EventArgs) Handles p.Exited Application.Exit() End Sub End Class Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
dannyres Posted October 14, 2003 Author Posted October 14, 2003 :D thanks divil that works perfect, but would you mind explaining how the Application stuff works? Dan Quote
*Gurus* divil Posted October 14, 2003 *Gurus* Posted October 14, 2003 The documentation can better than I can, but you use the Application class to start an application-wide message loop. Control does not return from calling Application.Run() until Application.Exit() is called so it's essential in cases like this where you application needs to be idle but not exit until you specify. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Mohsen Posted October 15, 2003 Posted October 15, 2003 Why don't u do this then ? I'm sure you've tried that at the beginnig, but what happened then? Friend Class Class1 Dim WithEvents p As Process Public Shared Sub Main() Dim ps As New ProcessStartInfo("notepad.exe") p = Process.Start(ps) p.EnableRaisingEvents = True End Sub Private Sub p_Exited(ByVal sender As Object, ByVal e As System.EventArgs) Handles p.Exited P.Exit() End Sub End Class I mean at the beginning, your start process seemed to take only one agrgument, and on exiting each one u needed to start another start process. Now you are taking the argument of the start proces as a file. So enough of threads, and Application class, no errors will be generated as long as u take the argument in a file? Keep me updated, Mohsen Quote
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.