<p>What's the best way to terminate a program and then run additional code from the program that's being terminated?  For example, what would be the best way for a program to self update itself?</p>

[{'answer_id': 46019, 'author': 'jodonnell', 'author_id': 4223, 'author_profile': 'https://Stackoverflow.com/users/4223', 'pm_score': 0, 'selected': False, 'text': "<p>I don't know of a way to do it without a second program that the primary program launches prior to shutting down.  Program 2 downloads and installs the changes and then relaunches the primary program.</p>\n"}, {'answer_id': 46023, 'author': 'Noah', 'author_id': 4539, 'author_profile': 'https://Stackoverflow.com/users/4539', 'pm_score': 0, 'selected': False, 'text': "<p>We did something like this in our previous app. We captured the termination of the program (in .NET 2.0) from either the X or the close button, and then kicked off a background update process that the user didn't see. It would check the server (client-server app) for an update, and if there was one available, it would download in the background using BITS. Then the next time the application opened, it would realize that there was a new version (we set a flag) and popped up a message alerting the user to the new version, and a button to click if they wanted to view the new features added to this version.</p>\n"}, {'answer_id': 46024, 'author': 'Brian R. Bondy', 'author_id': 3153, 'author_profile': 'https://Stackoverflow.com/users/3153', 'pm_score': 4, 'selected': True, 'text': "<p>You have a couple options:</p>\n\n<p>You could use another application .exe to do the auto update.  This is probably the best method.</p>\n\n<p>You can also rename a program's exe while it is running. Hence allowing you to get the file from some update server and replace it.  On the program's next startup it will be using the new .exe.  You can then delete the renamed file on startup. </p>\n"}, {'answer_id': 46026, 'author': 'crashmstr', 'author_id': 1441, 'author_profile': 'https://Stackoverflow.com/users/1441', 'pm_score': 0, 'selected': False, 'text': '<p>It makes it easier if you have a secondary app that runs to do the updates. You would execute the "updater" app, and then inside of it wait for the other process to exit. If you need access to the regular apps DLLs and such but they also need updating, you can run the updater from a secondary location with already updated DLLs so that they are not in use in the original location.</p>\n'}, {'answer_id': 46064, 'author': 'ninesided', 'author_id': 1030, 'author_profile': 'https://Stackoverflow.com/users/1030', 'pm_score': 1, 'selected': False, 'text': "<p>It'd be <em>really</em> helpful to know what language we're talking about here. I'm sure I could give you some really great tips for doing this in PowerBuilder or Cobol, but that might not really be what you're after! If you're talking Java however, then you could use a shut down hook - works great for me.</p>\n"}, {'answer_id': 46072, 'author': 'David Mohundro', 'author_id': 4570, 'author_profile': 'https://Stackoverflow.com/users/4570', 'pm_score': 0, 'selected': False, 'text': "<p>If you're using writing a .NET application, you might consider using ClickOnce. If you need quite a bit of customization, you might look elsewhere. </p>\n\n<p>We have an external process that performs updating for us. When it finds an update, it downloads it to a secondary folder and then waits for the main application to exit. On exit, it replaces all of the current files. The primary process just kicks the update process off every 4 hours. Because the update process will wait for the exit of the primary app, the primary app doesn't have to do any special processing other than start the update application.</p>\n\n<p>This is a side issue, but if you're considering writing your own update process, I would encourage you to look into using compression of some sort to (1) save on download and (2) provide one file to pull from an update server.</p>\n\n<p>Hope that makes sense!</p>\n"}, {'answer_id': 46081, 'author': 'Dillie-O', 'author_id': 71, 'author_profile': 'https://Stackoverflow.com/users/71', 'pm_score': 1, 'selected': False, 'text': '<p>Another thing to consider is that most of the "major" apps I\'ve been using (FileZilla, Paint.NET, etc.), are having the updaters uninstall the previous version of the app and then doing a fresh install of the new version of the application. </p>\n\n<p>I understand this won\'t work for really large applications, but this does seem to be a "preferred" process for the small to medium size applications.</p>\n'}]
['https://Stackoverflow.com/questions/46013', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4778/']