unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TRunThread = class(TThread)
private
FMainForm: TForm1;
FFileName: String;
public
constructor Create(MainForm: TForm1; FileName: String); overload;
procedure Execute; override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
constructor TRunThread.Create(MainForm: TForm1; FileName: String);
begin
inherited Create(False);
FFileName := FileName;
FMainForm := MainForm;
FreeOnTerminate := True;
end;
procedure TRunThread.Execute;
var
StartUpInfo: TStartUpInfo;
ProcessInfo: TProcessInformation;
ExitCode: LongWord;
begin
FillChar(StartUpInfo, SizeOf(StartUpInfo), 0);
StartUpInfo.cb := SizeOf(StartUpInfo);
if (CreateProcess(PChar(FFileName), nil, nil, nil, FALSE, 0, nil, nil, StartUpInfo, ProcessInfo)) then
begin
try
CloseHandle(ProcessInfo.hThread);
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
FMainForm.Enabled := True;
finally
CloseHandle(ProcessInfo.hProcess);
end
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
T: TRunThread;
begin
T := TRunThread.Create(Self, 'c:/winnt/notepad.exe');
Enabled := False;
end;
end.