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