Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • *Experts*
Posted

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

"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
Posted

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.

Posted

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

Posted

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.

 

;-)

Posted

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

Posted

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

  • *Gurus*
Posted

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

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

  • *Gurus*
Posted
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.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

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

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