关于OpenSemaphore函数(50分)

  • 主题发起人 主题发起人 yorkshi
  • 开始时间 开始时间
Y

yorkshi

Unregistered / Unconfirmed
GUEST, unregistred user!
OpenSemaphore函数有一个参数为dwDesiredAccess, 其值可取为SEMAPHORE_ALL_ACCESS.<br>不过我找了一些资料(包括Windows.pas), 还是不知道SEMAPHORE_ALL_ACCESS的具体值<br>为多少. 请问那位高手知道?
 
看看这个:<br><br>procedure TForm1.ShowProgress;<br>var<br>&nbsp; ICount: Integer; &nbsp; &nbsp;// general loop counter<br>begin<br>&nbsp; {wait for the semaphore, and get ownership}<br>&nbsp; WaitForSingleObject(SemaphoreHandle, INFINITE);<br><br>&nbsp; {display a visual indicator}<br>&nbsp; for ICount := 1 to 1000 do<br>&nbsp; begin<br>&nbsp; &nbsp; Gauge1.Progress := ICount;<br>&nbsp; end;<br><br>&nbsp; {release the semaphore}<br>&nbsp; ReleaseSemaphore(Form1.SemaphoreHandle, 1, nil);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; StartUpInfo: TStartUpInfo; &nbsp; &nbsp; &nbsp; &nbsp;// holds startup information<br>&nbsp; ProcessInfo: TProcessInformation; // holds process information<br>&nbsp; CurDir: string; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // holds the current directory<br>begin<br>&nbsp; {create the semaphore, nonsignaled}<br><br>&nbsp; SemaphoreHandle := CreateSemaphore(nil, 0, 2, 'MikesSemaphore');<br><br>&nbsp; {initialize the startup info structure}<br>&nbsp; FillChar(StartupInfo, SizeOf(TStartupInfo), 0);<br>&nbsp; with StartupInfo do<br>&nbsp; begin<br>&nbsp; &nbsp; cb := SizeOf(TStartupInfo);<br>&nbsp; &nbsp; dwFlags := STARTF_USESHOWWINDOW;<br>&nbsp; &nbsp; wShowWindow := SW_SHOWNORMAL;<br>&nbsp; end;<br><br>&nbsp; {launch the semaphore sibling program for the example}<br>&nbsp; CurDir := ExtractFilePath(ParamStr(0))+'ProjectOpenSemaphore.exe';<br><br>&nbsp; CreateProcess(PChar(CurDir), nil, nil, nil, False,<br>&nbsp; &nbsp; &nbsp; NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo);<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br>&nbsp; OldValue: DWORD; &nbsp;// holds the previous semaphore count<br>begin<br>&nbsp; {release the semaphore}<br>&nbsp; ReleaseSemaphore(SemaphoreHandle, 2, @OldValue);<br><br>&nbsp; {start the visual indication}<br>&nbsp; ShowProgress;<br>end;<br><br>The Semaphore Sibling Program<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; ICount: Integer; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // general loop counter<br>&nbsp; SemaphoreHandle: THandle; &nbsp; &nbsp; &nbsp;// holds the semaphore handle<br>&nbsp; PrevCount: DWORD; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// holds the previous semaphore counter<br>begin<br>&nbsp; {Open a handle to the semaphore}<br>&nbsp; SemaphoreHandle := OpenSemaphore($00f0000 or $00100000 or $3, FALSE,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'MikesSemaphore');<br><br>&nbsp; {wait to achieve ownership of the semaphore}<br>&nbsp; WaitForSingleObject(SemaphoreHandle, INFINITE);<br><br>&nbsp; {display a visual indication}<br>&nbsp; for ICount := 1 to 100000 do<br>&nbsp; begin<br>&nbsp; &nbsp; Gauge1.Progress := ICount;<br>&nbsp; end;<br><br>&nbsp; {release the semaphore}<br>&nbsp; ReleaseSemaphore(SemaphoreHandle, 1, @PrevCount);<br>end;<br>
 
接受答案了.
 
后退
顶部