以下是CreateProcess的例子
procedure TForm1.ShowProgress;
var
ICount: Integer;
// general loop counter
begin
{wait for the semaphore, and get ownership}
WaitForSingleObject(SemaphoreHandle, INFINITE);
{display a visual indicator}
for ICount := 1 to 1000do
begin
Gauge1.Progress := ICount;
end;
{release the semaphore}
ReleaseSemaphore(Form1.SemaphoreHandle, 1, nil);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
StartUpInfo: TStartUpInfo;
// holds startup information
ProcessInfo: TProcessInformation;
// holds process information
CurDir: string;
// holds the current directory
begin
{create the semaphore, nonsignaled}
SemaphoreHandle := CreateSemaphore(nil, 0, 2, 'MikesSemaphore');
{initialize the startup info structure}
FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
with StartupInfodo
begin
cb := SizeOf(TStartupInfo);
dwFlags := STARTF_USESHOWWINDOW;
wShowWindow := SW_SHOWNORMAL;
end;
{launch the semaphore sibling program for the example}
CurDir := ExtractFilePath(ParamStr(0))+'ProjectOpenSemaphore.exe';
CreateProcess(PChar(CurDir), nil, nil, nil, False,
NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
OldValue: DWORD;
// holds the previous semaphore count
begin
{release the semaphore}
ReleaseSemaphore(SemaphoreHandle, 2, @OldValue);
{start the visual indication}
ShowProgress;
end;
The Semaphore Sibling Program
procedure TForm1.Button1Click(Sender: TObject);
var
ICount: Integer;
// general loop counter
SemaphoreHandle: THandle;
// holds the semaphore handle
PrevCount: DWORD;
// holds the previous semaphore counter
begin
{Open a handle to the semaphore}
SemaphoreHandle := OpenSemaphore($00f0000 or $00100000 or $3, FALSE,
'MikesSemaphore');
{wait to achieve ownership of the semaphore}
WaitForSingleObject(SemaphoreHandle, INFINITE);
{display a visual indication}
for ICount := 1 to 100000do
begin
Gauge1.Progress := ICount;
end;
{release the semaphore}
ReleaseSemaphore(SemaphoreHandle, 1, @PrevCount);
end;