var Form1: TForm1; EventHandle: THandle; // holds the event handle ThreadHandle: THandle; // holds the thread handleimplementation{$R *.DFM}function ThreadFunction(Info: Pointer): Integer; stdcall; var FormDC: HDC; // holds a handle to the form device context Counter: Integer; // general loop counter CounterStr: string; // a string representation of the loop counter ObjRtn: Integer; // wait function return value begin {WaitForSingleObject will wait for the event to become signaled (ready to do something)} ObjRtn := WaitForSingleObject(EventHandle, INFINITE); {retrieve a handle to the form's device context} FormDC := GetDC(Form1.Handle); {begin a large loop} for Counter := 1 to 100000 do begin {display the counter value} CounterStr := IntToStr(Counter); TextOut(FormDC, 10, 10, PChar(CounterStr), Length(CounterStr)); {process any pending messages} Application.ProcessMessages; {this causes the loop to pause, as the PulseEvent function rapidly sets the event's signaled state to signaled and then unsignaled} ObjRtn := WaitForSingleObject(EventHandle, INFINITE); end; {release the form's device context and exit the thread} ReleaseDC(Form1.Handle, FormDC); ExitThread(4); end;procedure TForm1.Button1Click(Sender: TObject); var ThreadID: Integer; // holds the thread identifier begin {create a new thread} ThreadHandle := CreateThread(nil, 0, @ThreadFunction, nil, 0, ThreadId); end;procedure TForm1.Button2Click(Sender: TObject); begin {indicate that the event is signaled. this will cause the waiting thread to get past the WaitForSingleObject function, thus starting the loop} SetEvent(EventHandle); Label1.Caption := 'Event is signaled'; end;procedure TForm1.Button3Click(Sender: TObject); begin {reset the event object to a non signaled state. this will cause the thread loop to pause at the WaitForSingleObject function inside the loop} ResetEvent(EventHandle); Label1.Caption := 'Event is non signaled';end;procedure TForm1.Button4Click(Sender: TObject); begin {if the event has been reset (above), the thread's loop will be paused at the internal WaitForSingleObject function. PulseEvent will toggle the event's state from nonsignaled to signaled and back, causing the thread's loop to fire once.} PulseEvent(EventHandle); //Set to signaled and then nonsignaled Label1.Caption := 'signaled/nonsignaled';end;procedure TForm1.Button5Click(Sender: TObject); begin {create the event} EventHandle := CreateEvent(Nil, True, False, 'MyEvent'); end;The Tomes of Delphi 3: Win32 Core API Help File by Larry Diehl
以上是使用例子,以下是说明 Unit Windows.PasSyntax WaitForSingleObject( hHandle: THandle; {the handle of the object to wait for} dwMilliseconds: DWORD {the time-out interval in milliseconds} ): DWORD; {returns an event code}DescriptionThis function will check the current state of the specified object. The current thread will enter an efficient wait state if the object is non-signaled. After the wait condition is satisfied (i.e. the object becomes signaled), the thread resumes execution. In some circumstances, a wait function can specify a handle of a file, named pipe, or communications device as an object to wait for.Parameters hHandle: Specifies the handle of the object for which to wait.dwMilliseconds: Specifies the timeout period in milliseconds. The function will return after the specified timeout even if the object is non-signaled. If the parameter is set to zero, the function will test the object and return immediately. If this parameter is set to INFINITE, the timeout interval will never elapse.Return Value If the function succeeds, it returns a value. If the function fails, it returns WAIT_FAILED. To get extended error information, call the GetLastError function.The Tomes of Delphi 3: Win32 Core API Help File by Larry Diehl
在程序中调用另外的进程用的,用来等待另外一个进程完成。 hwnd := shellexecute(handle,nil,pchar('http://www.csdn.net'),nil,nil,sw_shownormal); if WaitForSingleObject(hwnd,0) then //调用程序结束,做一些自己的处理
Form1: TForm1;
EventHandle: THandle; // holds the event handle
ThreadHandle: THandle; // holds the thread handleimplementation{$R *.DFM}function ThreadFunction(Info: Pointer): Integer; stdcall;
var
FormDC: HDC; // holds a handle to the form device context
Counter: Integer; // general loop counter
CounterStr: string; // a string representation of the loop counter ObjRtn: Integer; // wait function return value
begin
{WaitForSingleObject will wait for the event to
become signaled (ready to do something)}
ObjRtn := WaitForSingleObject(EventHandle, INFINITE); {retrieve a handle to the form's device context}
FormDC := GetDC(Form1.Handle); {begin a large loop}
for Counter := 1 to 100000 do
begin
{display the counter value} CounterStr := IntToStr(Counter);
TextOut(FormDC, 10, 10, PChar(CounterStr), Length(CounterStr)); {process any pending messages}
Application.ProcessMessages; {this causes the loop to pause, as the PulseEvent function
rapidly sets the event's signaled state to signaled and
then unsignaled}
ObjRtn := WaitForSingleObject(EventHandle, INFINITE);
end; {release the form's device context and exit the thread} ReleaseDC(Form1.Handle, FormDC);
ExitThread(4);
end;procedure TForm1.Button1Click(Sender: TObject);
var
ThreadID: Integer; // holds the thread identifier
begin
{create a new thread}
ThreadHandle := CreateThread(nil, 0, @ThreadFunction, nil, 0, ThreadId);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
{indicate that the event is signaled. this will cause the waiting thread to get past the WaitForSingleObject function, thus starting
the loop}
SetEvent(EventHandle);
Label1.Caption := 'Event is signaled';
end;procedure TForm1.Button3Click(Sender: TObject);
begin
{reset the event object to a non signaled state. this will
cause the thread loop to pause at the WaitForSingleObject
function inside the loop}
ResetEvent(EventHandle);
Label1.Caption := 'Event is non signaled';end;procedure TForm1.Button4Click(Sender: TObject);
begin
{if the event has been reset (above), the thread's loop will be
paused at the internal WaitForSingleObject function. PulseEvent
will toggle the event's state from nonsignaled to signaled and back,
causing the thread's loop to fire once.}
PulseEvent(EventHandle); //Set to signaled and then nonsignaled
Label1.Caption := 'signaled/nonsignaled';end;procedure TForm1.Button5Click(Sender: TObject);
begin
{create the event}
EventHandle := CreateEvent(Nil, True, False, 'MyEvent');
end;The Tomes of Delphi 3: Win32 Core API Help File by Larry Diehl
Unit
Windows.PasSyntax
WaitForSingleObject(
hHandle: THandle; {the handle of the object to wait for}
dwMilliseconds: DWORD {the time-out interval in milliseconds}
): DWORD; {returns an event code}DescriptionThis function will check the current state of the specified object. The current thread will enter an efficient wait state if the object is non-signaled. After the wait condition is satisfied (i.e. the object becomes signaled), the thread resumes execution. In some circumstances, a wait function can specify a handle of a file, named pipe, or communications device as an object to wait for.Parameters
hHandle: Specifies the handle of the object for which to wait.dwMilliseconds: Specifies the timeout period in milliseconds. The function will return after the specified timeout even if the object is non-signaled. If the parameter is set to zero, the function will test the object and return immediately. If this parameter is set to INFINITE, the timeout interval will never elapse.Return Value
If the function succeeds, it returns a value. If the function fails, it returns WAIT_FAILED. To get extended error information, call the GetLastError function.The Tomes of Delphi 3: Win32 Core API Help File by Larry Diehl
hwnd := shellexecute(handle,nil,pchar('http://www.csdn.net'),nil,nil,sw_shownormal);
if WaitForSingleObject(hwnd,0) then
//调用程序结束,做一些自己的处理