Delphi 重启应用程序

在工程主文件中加入
Delay(500);

//启动程序时请延时一段时间,否则只能重启一次
procedure RestartApp;
var
BatchFile: TextFile;
BatchFileName: string;
ProcessInfo: TProcessInformation;
StartUpInfo: TStartupInfo;
begin
try
    BatchFileName := ExtractFilePath(ParamStr(0)) + '_D.bat';

    AssignFile(BatchFile, BatchFileName);
    Rewrite(BatchFile);

    Writeln(BatchFile, ExtractFileName(Application.ExeName));
    Writeln(BatchFile, 'del   %0');
    CloseFile(BatchFile);

    FillChar(StartUpInfo, SizeOf(StartUpInfo), $00);
    StartUpInfo.dwFlags := STARTF_USESHOWWINDOW;
    StartUpInfo.wShowWindow := SW_HIDE;

    if CreateProcess(nil, PChar(BatchFileName), nil, nil,
      False, IDLE_PRIORITY_CLASS, nil, nil, StartUpInfo,
      ProcessInfo) then
    begin
      CloseHandle(ProcessInfo.hThread);
      CloseHandle(ProcessInfo.hProcess);
    end;
    Application.Terminate;
except

end;
end;

posted @ 2013-05-01 16:50  小天1981  阅读(602)  评论(0编辑  收藏  举报