views:

2950

answers:

4

I have a VB6 application that needs to update it's self. For this purpose, the PM has recommended using a batch file that is to be launched from the application. The batch file should kill the process, download the new version from a local server, overwrite the old files and launch the application again.
My problem with this is that I am not sure that taskkill returns control the the parent only after the process has been killed and all the used resources have been released; in particular, we were wondering what happens to the process' .exe file. Is there any guarantee that it will be unlocked after taskkill returns? - of course, theoretically it shouldn't be locked by any other process, this is the only case we're interested in.

A: 

Have a command line parameter like /Shutdown that the VB program will process.

MyApp.exe /Shutdown

/Shutdown parameter will find existing running instance of the app (using FindWindow API) and send a user message to the window requesting it to shutdown smoothly.

goths
A: 

Taskkill should return only after the process has been terminated (as per comments below: use the /F flag to force termination of the program if it expects user input).

To learn more about it you can use Process explorer and see exactly when the process is terminated and also if anyone still has the .exe file open (Ctrl+F and the name of the exe) - but that shouldn't be the case.

rslite
The process has been terminated, but have the used resources been released? That is my question.
Tom
It doesn't, and it's simple to test. Open notepad and type something without saving it, then run taskkill /im notepad.exe. You'll see that taskkill returns control while notepad is asking if you want to save the document before closing.
Jeff Schumacher
If a process is terminated the resources (memory, file handles) should always be freed by the OS.
rslite
@Jeff - it does if you use the /F flag
rslite
@rslite - right, but that wasn't my point, my point was to prove that taskkill will return while the app is still running and trying to close.
Jeff Schumacher
+1  A: 

If the process is killed, then the exe will be freed up for the update. As an alternative to using taskkill, you can use pskill from sysinternals, but taskkill should work great with the /IM parameter.

However, neither pskill or taskkill will not wait for the process to stop before returning control. So you would have to monitor the processes to make sure they're no longer running.

Also, and this would be my preferred solution, it's possible to deploy a vb6 app via click-once, which will automatically update the app with new releases. Check this article for details.

As for the resources opened by the process, it's possible that they might remain open (such as a sql connection), but most likely they will be successfully closed. You'll only be able to know for sure by monitoring what happens by testing out taskkill on the process.

Hope that helps.

Jeff Schumacher
PSKill is "recognized" by some spyware / virusscanners as something bad. You should be aware of that when distributing the app.
GvS
@GvS - that's a very good point, and I've run into that same issue before with McAfee Enterprise.
Jeff Schumacher
A: 

I do this, by letting the app itself check for updates. If there are, then the app downloads them in a temporary place. After that it will start a (.js) script and exits the app. This script will wait for the process to close (sleep), copy the updates and start the app again.

This way you have far less logic in the script, and you can do your stuff in an environment you have more tools available.

GvS