function IsThreadTerminated( Handle: THandle ): Boolean;
var
lwExitCode: LongWord;
begin
GetExitCodeThread( Handle, lwExitCode );
Result := STILL_ACTIVE<>lwExitCode;
end;
procedure WaitForThread( AThread: TThread ); // 这个与上面的函数可以公用出去
begin
if not IsThreadTerminated( AThread.Handle ) then
Application.ProcessMessages; // 如果没有结束,则让应用程序处理消息,如WM_PAINT
end;
procedure TChangeNotifyThread.Execute;
var
hand: THandle;
begin
// 我们的例子是要监视C盘根目录下文件名是否改变、是否有文件复制和删除
hand := FindFirstChangeNotification( 'c:\', FALSE, // 第一次创建句柄
FILE_NOTIFY_CHANGE_FILE_NAME );
while not Terminated do // 别忘了检测这个属性
begin
if WaitForSingleObject( hand, 2000 )=WAIT_OBJECT_0 then // 等待变化
begin
Synchronize( TriggerButton ); // 调用Button.Click
FindNextChangeNotification( hand ); // 等待下次的变化
end;
end;
FindCloseChangeNotification( hand ); // 关闭句柄
end;
procedure TChangeNotifyThread.TriggerButton;
begin
Button.Click;
end;
3)例如你的button1的click事件是
procedure TForm1.Button1OnClick(Sender : TObject);
begin
// your code here.
end;
直接调用就可以
procedure DoButtonClick;// your function
begin
Button1OnClick(self);
end;
end;