Killing parent process without closing child process?

Posted on 2011-10-16
Last Modified: 2012-05-12
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?

Question by:gem56
    LVL 9

    Expert Comment

       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"
        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End Sub
    LVL 83

    Expert Comment

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

    Expert Comment

    by:käµfm³d 👽
    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.

    Author Comment

    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?
    LVL 16

    Accepted Solution


    I would try to Win32 API CreateProcess. Dont know what Process.Start() is doing in the background, but CreateProcess allows to run new independent processes: and

    Here is a .Net wrapper for CreateProcess

    So, the updater exe is now a separate process.

    The updater can 'kill' the main app the hard way or use some interprocess communication. You may use a flag file that is checked by the main app periodically or use a private message or event to inform the main app to shutdown itself. The updater can then re-launch the main app after updating files and then terminate itself.

    Here is another example, if you use a internet search with " update exe" you will find some more.

    Author Comment

    champion, that works nicely, thank you :)

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
    This is about my first experience with programming Arduino.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now