type
TThreadWithMsg = class(TThread)
private
{ Private declarations }
procedure WMUser(var Msg: TWMNoParams); message WM_USER;
protected
procedure Execute; override;
end;
{ TThreadWithMsg }
procedure TThreadWithMsg.Execute;
var
Msg: TMsg;
DMsg: TMessage;
begin
PeekMessage(Msg,0,0,0,PM_NOREMOVE); // Create Message Queue
while not Terminated do begin
Sleep(10); // don't eat CPU cycles
if PeekMessage(Msg,0,0,0,PM_REMOVE) then begin
DMsg.Msg:=Msg.message;
DMsg.wParam:=Msg.wParam;
DMsg.lParam:=Msg.lParam;
DMsg.Result:=0;
Dispatch(DMsg);
end;
end;
end;
procedure TThreadWithMsg.WMUser(var Msg: TWMNoParams);
begin
Beep(440,100);
end;
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
MyThread:=TThreadWithMsg.Create(False);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
MyThread.Terminate;
MyThread.WaitFor;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
PostThreadMessage(MyThread.ThreadID,WM_USER,0,0);
end;
ไม่มีความคิดเห็น:
แสดงความคิดเห็น