在执行 ShellExecute 之前等待?
我有一个希望很快的问题:是否可以稍微延迟 ShellExecute 的执行?
我有一个带有自动更新程序的应用程序。下载所有必需的文件等后,它将当前文件重命名为 *.OLD,并将新文件重命名为以前的文件。够简单的。但随后我需要删除那些 .OLD 文件。这个“清理”过程在 MainForm.OnActivate 上执行(检查它是否是第一个激活过程)。但这显然发生得太快了(我从DeleteFile中得到False)。这是过程:
procedure TUpdateForm.OKBtnClick(Sender: TObject);
const SHELL = 'ping 127.0.0.1 -n 2';
begin
ShellExecute(0,'open',pchar(SHELL+#13+Application.ExeName),nil,nil,SW_SHOWNORMAL);
Application.Terminate;
end;
此过程应该重新启动应用程序。我确信删除问题是由第二个应用程序的快速启动引起的,因为如果我自己重新启动它,给它一点时间,文件就会正常删除。
tl;dr 版本:我需要调用 ShellExecute(),它会等待一段时间(0.1 秒左右),然后执行命令。
注意
我尝试使用 -ping 命令来尝试延迟它,但没有成功。
提前非常感谢您
编辑:重新措辞
我需要做到这一点|| 第一个应用程序关闭;等待100毫秒;第二个应用程序打开 ||。我需要先调用 ShellExecute,然后等到调用应用程序完全关闭自身,然后执行 shell(即打开第二个应用程序)
I have a hopefully quick question: Is it possible to delay execution of ShellExecute a little bit?
I have an application with autoupdater. After it downloads all necessary files etc, it renames current files to *.OLD and the new as the previous. Simple enough. But then I need to delete those .OLD files. This 'cleanup' procedure is executed on MainForm.OnActivate (with a check if it is the first activate proc). But this apparently happens too fast (I get False from DeleteFile). This is the procedure:
procedure TUpdateForm.OKBtnClick(Sender: TObject);
const SHELL = 'ping 127.0.0.1 -n 2';
begin
ShellExecute(0,'open',pchar(SHELL+#13+Application.ExeName),nil,nil,SW_SHOWNORMAL);
Application.Terminate;
end;
This procedure is supposed to restart the application. I am certain that the deleting problem is caused by the quick start of the second application, because if I restart it myself, giving it a little time, the files get deleted normally.
tl;dr version: I need to call ShellExecute() which waits a bit (0.1 sec or so) and THEN executes the command.
Note
I tried using the -ping command to try to delay it, but it didn't work.
Thank you very much in advance
Edit: Rephrased
I need this to happen || First app closes; Wait 100 ms; second app opens ||. I need to call ShellExecute first, then wait until the calling application closes itself completely, then execute the shell (i.e. open second application)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你正在做自动修补程序,对吗?
我遇到了同样的问题,这就是我绕过它的方法:
您使用参数“--delay”或类似的东西运行第二个应用程序。
第二个应用程序处理参数“--delay”并休眠 100 毫秒,然后继续正常运行。
You're doing an autopatcher right ?
I've had the same problem and this is how I bypassed it :
You run second app with argument "--delay" or something like that.
Second app handles argument "--delay" and sleeps for 100 ms, then continues running normally.
这个例程是我们游戏引擎中的一些实用程序代码。它可以运行可执行文件并可选择等待其退出。它将返回其退出代码:
以下是一些可以检查进程是否存在的代码。所以...当前应用程序调用更新程序并终止。更新程序可以检查旧应用程序是否已终止并执行操作(重命名、更新、删除等):
This routine is some utils code in our game engine. It can run an executable and optionally wait for it to exit. It will return its exit code:
Here is some code that can check to see if a process exists. So... current app calls the updater and terminates. The updater can check to see if old app has terminated and do it's thing (rename, update, delete, etc):
如果您可以使用
CreateProcess
而不是ShellExecute
,则可以等待进程句柄。当应用程序退出时,进程句柄会收到信号。例如:ExecAndWait返回后,如果需要,可以休眠100ms。
氮@
If you can use
CreateProcess
instead ofShellExecute
, you can wait on the process handle. The process handle is signalled when the application exits. For example:After ExecAndWait returns, then you can sleep for 100ms if you need to.
N@