关闭并重新启动DELPHI中的当前应用程序
我怎样才能做到这一点?
由于某种原因或由用户选择,“要求”当前应用程序自行重新启动。
How can I do this one?
For some reason or selected by the user, “ask” the current application to restart it self.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
还有另一种关闭-重启应用程序的方法:
将计划任务保存到应用程序关闭后的短时间内。这必须是您的应用程序在退出之前执行的最后操作(没有进一步的数据处理、保存、上传或其他操作),
例如。
当您的程序再次启动时,它应该检查任何此类计划任务并将其删除。这必须是您的应用程序在启动时应该执行的非常第一操作。 (清理)
据我所知,Delphi Jedi 组件集有一个组件可以用来执行任务计划。
There is another way for closing-restarting the application:
Save a scheduled task to a short time after the application closes. This will have to be the VERY LAST thing your application does before exiting (no further data processing, saving, uploading or whatever)
eg.
When your program starts again, it should check for any such scheduled task and remove them. This must be the VERY FIRST action your application should do when starting. (cleanup)
AFAIK, The Delphi Jedi component set has a component you can do the task scheduling stuff with.
我可以添加 @Andreas Rejbrand 答案:
如果代码在
Application.Initialize.
之前在 .dpr 文件中执行,对我来说,最好调用Halt(0)
而不是应用程序.终止
。我需要它,因为在第一次启动程序时会进行一些安装,然后重新启动。如果我调用Application.Terminate
主窗体会闪烁一秒钟(但根本不应该创建它!),然后应用程序关闭。所以我的代码如下:project.dpr:
I can add to @Andreas Rejbrand answer:
If the code is perfomed in .dpr file before
Application.Initialize.
for me it is better to callHalt(0)
instead ofApplication.Terminate
. I need it because on the first launch program does some installation and then restarts. If I callApplication.Terminate
main form blinks for a seconds (but it shouldn't be created at all!) and then application closes. So my code is following:project.dpr:
您可以有一个单独的简单的 restart.exe 程序,您可以从您的程序中运行该程序,并将可执行文件的名称传递给它。然后关闭你的程序。重启程序可以等待一段时间,或者直到可执行文件可读写(这似乎意味着它没有运行),然后它可以执行它并自行关闭。
我希望有更好的方法来做到这一点,也许有人可以提供更好的解决方案,但这个函数似乎告诉我当前是否正在运行可执行文件:
You could have a separate simple restart.exe program that you run from your program and pass it the name of your executable file. Then close your program. The restart program can wait for a time, or until the executable file is read-writeable which seems to mean it is not running, then it can execute it and close itself.
I expect there is a better way to do this, maybe sombody can provide a better solution, but this function seems to tell me whether an executable is currently running: