Problem With formless app

dannyres

Regular
Joined
Aug 29, 2003
Messages
67
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:

Visual Basic:
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
 
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
 
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.
 
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
 
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.

;-)
 
hmm Engine252 i dont really get what your saying there... any chance you could whip up an example for me?




Dan
 
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
 
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
 
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
 
This will do what you need.

Visual Basic:
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
 
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.
 
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
 
Back
Top