在工程文件中添加如下代码即可;运行很完美
var
lpStartupInfo : TStartupInfo;
lpProcessInformation : TProcessInformation;
begin
Application.Initialize;
//创建主窗体或其他的事件
Application.Run;
//Restart_Flag变量需要在另外的公用单元中申请,以方便其他单元调用
if not Restart_Flag then Exit; //不需要重启
//自动重启程序本身
FillChar(lpStartupInfo, Sizeof(lpStartupInfo), $00);
FillChar(lpProcessInformation, Sizeof(lpProcessInformation), $00);
lpStartupInfo.cb := Sizeof(lpStartupInfo);
if CreateProcess(nil , PChar(Paramstr(0)), nil, nil, false, 0, nil, nil, lpStartupInfo, lpProcessInformation) then
begin
CloseHandle(lpProcessInformation.hThread);
CloseHandle(lpProcessInformation.hProcess);
end;
end.
Pascal