Link to home
Start Free TrialLog in
Avatar of gem56
gem56

asked on

Killing parent process without closing child process?

hi guys,

i have two application, application A is the main program and application B which is a little updater app. Now what i do is from my main application i run the updater app to check for updates and if one is found i basically overwrite application A with the new version.

my problem is that i cant have application A opened/running while i attempt to replace the file and hence need a way to close the application. i've been playing around with Process.Kill() however whenever i use it, both my applications close (i've read that as application B is opened by application A, A is B's parent and therefore calling Process.Kill() on A will close B as well as .Kill() terminates the entire process tree). what is the alternative to close A and keep B alive?

thanks
Avatar of raysonlee
raysonlee

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim job As System.Diagnostics.Process = New System.Diagnostics.Process()
        job.StartInfo.UseShellExecute = True
        job.StartInfo.Arguments += " /K TITLE Command Prompt"
        job.StartInfo.FileName = "c:\\windows\\system32\\cmd.exe"
        job.Start()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
Avatar of Nasir Razzaq
One option is to launch the update app. It checks to see if there is an update and updates the app and then launches it. If no update then it just launches the app. You can call it launcher rather than updater. Or  you can create another Launcher process which launches the updater first and then the app. This way, you wont have to make much modifications to existing code.
How are you launching the update app? Via Process.Start?

I wrote an update application some time back and my approach was to have the main application launch the update app via Process.Start. The update app would accept the path to the main app as well as the path to the updated exe (i.e. the location on the server), and the process ID of the currently running main executable. I compare the file versions of client and server; if the server is more recent, I would loop through the running processes on the client attempting to match the process ID that I passed on the command line. Once matched, I would kill that process (i.e. the main executable) and perform the copy. Once the copy completed, I had the path to the client executable, so I again used Process.Start and I relaunched the client.

This is dependent on not having multiple instances of the main app going, since the executable's file would be locked and prevent copying the updated version. You could modify this logic to account for multiple instances, though.

If you'd like to see the code I made I can post it, but I'll have to convert it to VB.
Avatar of gem56

ASKER

hey guys, thanks for the replies.

@kaufmed: thats exactly the process im using...without restarting the application at the end after the update. in my main application i dim a new process, give it some processstartinfo values then process.start(). my updater then checks what it needs to and if theres a newer version on the server it'll download, unzip and prepare for an install. at this point it then attempts to process.kill() the main application to overwrite its files.

the absolutely puzzling thing is that yesterday when i was doing all this, whenever i called process.kill() both my main application and updater app would both close. after testing around and calling Process.GetCurrentProcess().ProcessName in both application, they both returned the exact same name (the name of the main application)...even though both .exe's are named differently. after googling around i read that when one process starts another, the second process will start "under" the calling process as if creating a process tree with the calling process being the root.

the confusing thing is that today when i ran the exact same procedure, it seems to work perfectly? calling Process.GetCurrentProcess().ProcessName now returns two different names (one being the main application name, the other being the updater application name). now im totally confused as im getting two completely different results with the exact same code?

any ideas?
cheers
ASKER CERTIFIED SOLUTION
Avatar of hjgode
hjgode
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gem56

ASKER

champion, that works nicely, thank you :)