第一步:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
//加一个重启标志
Restart_Flag: Boolean = false;
implementation
{$R *.dfm}
end.
第二步:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
//添加一个“重启”按钮
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
//加一个重启标志
Restart_Flag: Boolean = false;
implementation
{$R *.dfm}
//启动按钮的执行代码
procedure TForm4.Button1Click(Sender: TObject);
begin
Restart_Flag := True;
Close;
end;
end.
第三步:
program Project1;
uses
windows,
Forms,
Unit1 in 'Unit1.pas' {Form4};
{$R *.res}
var
lpStartupInfo: TStartupInfo;
lpProcessInformation: TProcessInformation;
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.Run;
//添加重启代码
if Not Restart_Flag then Exit; //不需要重启
FillChar( lpStartupInfo,sizeof(lpStartupInfo),0);
FillChar(lpProcessInformation,sizeof(lpProcessInformation),0);
lpStartupInfo.cb:=sizeof(lpStartupInfo);
if CreateProcess(nil,PChar(Application.ExeName),nil,nil,false,0,nil,nil,lpStartupInfo,lpProcessInformation) then
begin
CloseHandle(lpProcessInformation.hThread);
CloseHandle(lpProcessInformation.hProcess);
end;
end.
如果是在程序当中有使用互斥对象,也可以在Application.Initialize前初始化,Application.Run之后清理
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
//加一个重启标志
Restart_Flag: Boolean = false;
implementation
{$R *.dfm}
end.
第二步:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
//添加一个“重启”按钮
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
//加一个重启标志
Restart_Flag: Boolean = false;
implementation
{$R *.dfm}
//启动按钮的执行代码
procedure TForm4.Button1Click(Sender: TObject);
begin
Restart_Flag := True;
Close;
end;
end.
第三步:
program Project1;
uses
windows,
Forms,
Unit1 in 'Unit1.pas' {Form4};
{$R *.res}
var
lpStartupInfo: TStartupInfo;
lpProcessInformation: TProcessInformation;
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.Run;
//添加重启代码
if Not Restart_Flag then Exit; //不需要重启
FillChar( lpStartupInfo,sizeof(lpStartupInfo),0);
FillChar(lpProcessInformation,sizeof(lpProcessInformation),0);
lpStartupInfo.cb:=sizeof(lpStartupInfo);
if CreateProcess(nil,PChar(Application.ExeName),nil,nil,false,0,nil,nil,lpStartupInfo,lpProcessInformation) then
begin
CloseHandle(lpProcessInformation.hThread);
CloseHandle(lpProcessInformation.hProcess);
end;
end.
如果是在程序当中有使用互斥对象,也可以在Application.Initialize前初始化,Application.Run之后清理