Delphi 重启应用程序(创建Bat文件的Process)

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 @ 2015-12-14 14:15  findumars  Views(1226)  Comments(0Edit  收藏  举报