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-Deklarationen }
procedure RunProcessExit(Sender: TObject);
public
{ Public-Deklarationen }
end;
TRunProcessThread =
class(TThread)
private
FFileName:
String;
FOnProcessExit: TNotifyEvent;
procedure UpdateVCL;
protected
procedure Execute;
override;
public
constructor Create;
property FileName:
String read FFileName
write FFileName;
property OnProcessExit: TNotifyEvent
read FOnProcessExit
write FOnProcessExit;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TRunProcessThread }
constructor TRunProcessThread.Create;
begin
inherited Create(True);
end;
procedure TRunProcessThread.Execute;
var
SI: TStartupInfo;
PI: TProcessInformation;
begin
FillChar(SI, SizeOf(TStartupInfo), 0);
SI.cb := SizeOf(TStartupInfo);
if CreateProcess(
nil, PChar(FFileName),
nil,
nil, False, 0,
nil,
nil, SI, PI)
then
begin
WaitForSingleObject(PI.hProcess, INFINITE);
if Assigned(FOnProcessExit)
then
Synchronize(UpdateVCL);
CloseHandle(PI.hProcess);
CloseHandle(PI.hThread);
end;
end;
procedure TRunProcessThread.UpdateVCL;
begin
FOnProcessExit(Self);
end;
{ TForm1 }
procedure TForm1.RunProcessExit(Sender: TObject);
begin
ShowMessage('
Programm wurde beendet');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
with TRunProcessThread.Create
do
begin
FreeOnTerminate := True;
OnProcessExit := RunProcessExit;
FileName := '
C:\WINDOWS\SYSTEM32\NOTEPAD.EXE';
Resume;
end;
end;
end.