关闭并重新启动DELPHI中的当前应用程序

发布于 2024-11-07 05:58:43 字数 57 浏览 4 评论 0原文

我怎样才能做到这一点?

由于某种原因或由用户选择,“要求”当前应用程序自行重新启动。

How can I do this one?

For some reason or selected by the user, “ask” the current application to restart it self.

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

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

发布评论

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

评论(4

傲世九天 2024-11-14 05:58:43
uses ShellAPI;

...

procedure TForm1.RestartThisApp;
begin
  ShellExecute(Handle, nil, PChar(Application.ExeName), nil, nil, SW_SHOWNORMAL);
  Application.Terminate; // or, if this is the main form, simply Close;
end;
uses ShellAPI;

...

procedure TForm1.RestartThisApp;
begin
  ShellExecute(Handle, nil, PChar(Application.ExeName), nil, nil, SW_SHOWNORMAL);
  Application.Terminate; // or, if this is the main form, simply Close;
end;
晨与橙与城 2024-11-14 05:58:43

还有另一种关闭-重启应用程序的方法:

将计划任务保存到应用程序关闭后的短时间内。这必须是您的应用程序在退出之前执行的最后操作(没有进一步的数据处理、保存、上传或其他操作),

例如。

  1. 获取系统时间 首先
  2. 在这个时间之后设置计划任务(计划事件将必须启动您的可执行文件)
  3. 退出您的应用程序(关闭主窗体或 application.terminate 会执行此操作)

当您的程序再次启动时,它应该检查任何此类计划任务并将其删除。这必须是您的应用程序在启动时应该执行的非常第一操作。 (清理)

  1. 检查由可执行文件创建的任何计划任务,
  2. 删除它们

据我所知,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.

  1. get the system time first
  2. set the scheduled task some time after this time (scheduled event will have to start your executable)
  3. exit your application (closing the main form or application.terminate will do it)

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)

  1. check for any scheduled tasks created by your executable
  2. remove them

AFAIK, The Delphi Jedi component set has a component you can do the task scheduling stuff with.

ˇ宁静的妩媚 2024-11-14 05:58:43

我可以添加 @Andreas Rejbrand 答案:

如果代码在 Application.Initialize. 之前在 .dpr 文件中执行,对我来说,最好调用 Halt(0) 而不是应用程序.终止。我需要它,因为在第一次启动程序时会进行一些安装,然后重新启动。如果我调用 Application.Terminate 主窗体会闪烁一秒钟(但根本不应该创建它!),然后应用程序关闭。所以我的代码如下:

project.dpr:

if FirstRun then
begin
    DoFirstRunStuff();
    ShellExecute(Application.Handle, nil, PChar(Application.ExeName), nil, nil, SW_SHOWNORMAL);
    Halt(0);
end;
...
Application.Initialize;
Application.CreateForm(TMainForm, MainForm);
Application.Run;

I can add to @Andreas Rejbrand answer:

If the code is perfomed in .dpr file before Application.Initialize. for me it is better to call Halt(0) instead of Application.Terminate. I need it because on the first launch program does some installation and then restarts. If I call Application.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:

if FirstRun then
begin
    DoFirstRunStuff();
    ShellExecute(Application.Handle, nil, PChar(Application.ExeName), nil, nil, SW_SHOWNORMAL);
    Halt(0);
end;
...
Application.Initialize;
Application.CreateForm(TMainForm, MainForm);
Application.Run;
救星 2024-11-14 05:58:43

您可以有一个单独的简单的 restart.exe 程序,您可以从您的程序中运行该程序,并将可执行文件的名称传递给它。然后关闭你的程序。重启程序可以等待一段时间,或者直到可执行文件可读写(这似乎意味着它没有运行),然后它可以执行它并自行关闭。

我希望有更好的方法来做到这一点,也许有人可以提供更好的解决方案,但这个函数似乎告诉我当前是否正在运行可执行文件:

function CanReadWriteFile(const f: TFileName): boolean;
var
  i: integer;
begin
  Result := false;
  i := FileOpen(f, fmOpenReadWrite);
  if i >= 0 then begin
    Result := true;
    FileClose(i);
  end;
end;

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:

function CanReadWriteFile(const f: TFileName): boolean;
var
  i: integer;
begin
  Result := false;
  i := FileOpen(f, fmOpenReadWrite);
  if i >= 0 then begin
    Result := true;
    FileClose(i);
  end;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文