从 .bat 文件安全地终止进程

发布于 2024-07-07 03:34:23 字数 236 浏览 10 评论 0原文

我有一个 VB6 应用程序需要自我更新。 为此,PM 建议使用从应用程序启动的批处理文件。 批处理文件应该终止进程,从本地服务器下载新版本,覆盖旧文件并再次启动应用程序。
我的问题是,我不确定taskkill仅在进程被终止并且所有使用的资源被释放后才返回父进程的控制权; 特别是,我们想知道进程的 .exe 文件会发生什么。 有没有保证taskkill返回后会解锁? - 当然,理论上它不应该被任何其他进程锁定,这是我们唯一感兴趣的情况。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清晰传感 2024-07-14 03:34:23

如果进程被终止,那么 exe 将被释放以进行更新。 作为使用 taskkill 的替代方法,您可以使用 sysinternals 中的 pskill,但 taskkill 应该与 /IM 参数配合使用。

但是, pskill 或 taskkill 都不会等待进程停止后再返回控制权。 因此,您必须监视进程以确保它们不再运行。

另外,这将是我的首选解决方案,可以通过单击一次来部署 vb6 应用程序,这将自动使用新版本更新应用程序。 有关详细信息,请参阅本文

至于进程打开的资源,它们有可能保持打开状态(例如sql连接),但很可能它们会成功关闭。 您只能通过测试进程上的 taskkill 来监控发生的情况才能确定。

希望有帮助。

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.

这个俗人 2024-07-14 03:34:23

我通过让应用程序本身检查更新来做到这一点。 如果有,应用程序会将它们下载到临时位置。 之后它将启动 (.js) 脚本并退出应用程序。 该脚本将等待进程关闭(睡眠),复制更新并再次启动应用程序。

这样,脚本中的逻辑就会少得多,并且您可以在拥有更多可用工具的环境中完成工作。

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.

老娘不死你永远是小三 2024-07-14 03:34:23

有 VB 程序将处理的命令行参数,如 /Shutdown。

MyApp.exe /Shutdown

/Shutdown 参数将查找应用程序的现有运行实例(使用 FindWindow API)并向窗口发送用户消息,请求其顺利关闭。

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.

分分钟 2024-07-14 03:34:23

Taskkill 应该仅在进程终止后返回(根据下面的注释:如果需要用户输入,请使用 /F 标志强制终止程序)。

要了解有关它的更多信息,您可以使用 进程资源管理器 并准确查看何时该进程将终止,并且如果有人仍然打开 .exe 文件(Ctrl+F 和 exe 的名称) - 但情况不应该如此。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文