多线程处理消息问题 ( 积分: 100 )

  • 主题发起人 主题发起人 lanm
  • 开始时间 开始时间
L

lanm

Unregistered / Unconfirmed
GUEST, unregistred user!
type
ThdRec = class(TThread)
private
FOwner:TControl;
FOwnerProc:TWndMethod;
FMessage:TMessage;
FEvent :TSimpleEvent;
procedure ThreadWndProc(var Message:TMessage);
constructor Create(AOwner:TControl);
destructor Destroy;
procedure ReadData;
protected
procedure Execute;
override;
end;

implementation
uses UnitTCPUDP;
{ ThdRec }
constructor ThdRec.Create(AOwner:TControl);
begin
inherited Create(True);
FOwner:=AOwner;
if Assigned(FOwner) then
begin
FOwnerProc:=AOwner.WindowProc;
FOwner.WindowProc:=ThreadWndProc;
end;
FEvent:=TSimpleEvent.Create;
end;

destructor ThdRec.Destroy;
begin
if Assigned(FOwner) then
FOwner.WindowProc:=FOwnerProc;

FEvent.Free;
inherited Destroy;
end;

procedure ThdRec.ThreadWndProc(var Message:TMessage);
begin
if Message.Msg=WM_DESTROY then
begin
Terminate;
FMEssage:=Message;
FEvent.SetEvent;
FOwner.WindowProc:=FOwnerProc;
FOwnerProc:=nil;
FOwner:=nil;
end;

if (Message.Msg>=WM_SOCK) and (Message.Msg<=WM_SOCK+MAX_ACCEPT) then
begin
FMessage:=Message;
FEvent.SetEvent;
end
else
if Assigned(FOwnerProc) then
FOwnerProc(Message);
end;

procedure ThdRec.Execute;
begin
while not Terminateddo
begin
FEvent.WaitFor(INFINITE);

FEvent.ResetEvent;
ReadData;


end
end;

这段代码我在网上找的,用来实现线程中处理消息,运行到FEvent.WaitFor(INFINITE);
这句老是出错,各位大侠有能否告诉我错在哪里?或者有什么别的方法实现线程中处理消息.
谢谢!!!
 
试试这样
if FEvent > 0 then
WaitForSingleObject(FEvent, INFINITE);
 
后退
顶部