Q
qier_7
Unregistered / Unconfirmed
GUEST, unregistred user!
我的线程接受数据并处理。线程是通过接受消息来处理数据的。但是,他好像会漏掉一些消息。这个线程在程序开始时就创建,和主线程并行执行。
以下的两种方法都会漏掉消息:
1。
thrdreceivedata = class(TThread)
private
Fmessage : TMessage;
Fevent : TSimpleEvent;
Fowner : TControl;
FOwnerProc : TWndMethod;
procedure ThreadWndProc(var mesg : TMessage);
protected
procedure Execute;
override;
public
constructor create(Aowner:TControl);
end;
implementation
constructor thrdreceivedata.create(Aowner:TControl);
var
i:integer;
begin
FOwner:=AOwner;
// Aowner:传递的参数
if Assigned(FOwner) then
// Assigned:函数,返回是否为空
begin
//如果Fowner不为空
FOwnerProc:=AOwner.WindowProc;
FOwner.WindowProc:=ThreadWndProc;
end;
FEvent:=TSimpleEvent.Create;
inherited create(false);
end;
procedure thrdreceivedata.Execute;
begin
//
while not terminateddo
begin
FEvent.WaitFor(20000);
//wait for fevent todo
if FMessage.Msg=RECVMESS then
begin
//处理数据
FMessage.Msg:=0;
deal;
end;
FEvent.ResetEvent;
end;
end;
procedure thrdreceivedata.ThreadWndProc(var mesg: TMessage);
var
i:integer;
begin
with mesgdo
begin
if Msg=WM_DESTROY then
begin
FOwner.WindowProc:=FOwnerProc;
FOwnerProc:=nil;
FOwner:=nil;
end
else
if msg=RECVMESS then
begin
FMessage:=Mesg;
FEvent.SetEvent;
end
else
if Assigned(FOwnerProc) then
FOwnerProc(Mesg);
end;
end;
2。
thrdreceivedata = class(TThread)
private
Fmessage : TMessage;
Fevent : TSimpleEvent;
protected
procedure Execute;
override;
procedure receivemessage(var mesg : TMessage);
public
FWindowhandle:HWND;
constructor create();
constructor thrdreceivedata.create;
var
i:integer;
begin
FWindowhandle:=AllocateHWnd(receivemessage);
FEvent:=TSimpleEvent.Create;
inherited create(false);
end;
procedure thrdreceivedata.Execute;
begin
//
while not terminateddo
begin
FEvent.WaitFor(20000);
//wait for fevent todo
if FMessage.Msg=RECVMESS then
begin
//处理数据
FMessage.Msg:=0;
deal;
end;
FEvent.ResetEvent;
end;
end;
procedure thrdreceivedata.receivemessage(var mesg: TMessage);
var
i:integer;
begin
//虚拟窗口接收消息
with mesgdo
begin
if msg=RECVMESS then
begin
FMessage:=Mesg;
FEvent.SetEvent;
end
else
Result := DefWindowProc(FWindowHandle, Msg, wParam, lParam);
end;
end;
望高手指教!
以下的两种方法都会漏掉消息:
1。
thrdreceivedata = class(TThread)
private
Fmessage : TMessage;
Fevent : TSimpleEvent;
Fowner : TControl;
FOwnerProc : TWndMethod;
procedure ThreadWndProc(var mesg : TMessage);
protected
procedure Execute;
override;
public
constructor create(Aowner:TControl);
end;
implementation
constructor thrdreceivedata.create(Aowner:TControl);
var
i:integer;
begin
FOwner:=AOwner;
// Aowner:传递的参数
if Assigned(FOwner) then
// Assigned:函数,返回是否为空
begin
//如果Fowner不为空
FOwnerProc:=AOwner.WindowProc;
FOwner.WindowProc:=ThreadWndProc;
end;
FEvent:=TSimpleEvent.Create;
inherited create(false);
end;
procedure thrdreceivedata.Execute;
begin
//
while not terminateddo
begin
FEvent.WaitFor(20000);
//wait for fevent todo
if FMessage.Msg=RECVMESS then
begin
//处理数据
FMessage.Msg:=0;
deal;
end;
FEvent.ResetEvent;
end;
end;
procedure thrdreceivedata.ThreadWndProc(var mesg: TMessage);
var
i:integer;
begin
with mesgdo
begin
if Msg=WM_DESTROY then
begin
FOwner.WindowProc:=FOwnerProc;
FOwnerProc:=nil;
FOwner:=nil;
end
else
if msg=RECVMESS then
begin
FMessage:=Mesg;
FEvent.SetEvent;
end
else
if Assigned(FOwnerProc) then
FOwnerProc(Mesg);
end;
end;
2。
thrdreceivedata = class(TThread)
private
Fmessage : TMessage;
Fevent : TSimpleEvent;
protected
procedure Execute;
override;
procedure receivemessage(var mesg : TMessage);
public
FWindowhandle:HWND;
constructor create();
constructor thrdreceivedata.create;
var
i:integer;
begin
FWindowhandle:=AllocateHWnd(receivemessage);
FEvent:=TSimpleEvent.Create;
inherited create(false);
end;
procedure thrdreceivedata.Execute;
begin
//
while not terminateddo
begin
FEvent.WaitFor(20000);
//wait for fevent todo
if FMessage.Msg=RECVMESS then
begin
//处理数据
FMessage.Msg:=0;
deal;
end;
FEvent.ResetEvent;
end;
end;
procedure thrdreceivedata.receivemessage(var mesg: TMessage);
var
i:integer;
begin
//虚拟窗口接收消息
with mesgdo
begin
if msg=RECVMESS then
begin
FMessage:=Mesg;
FEvent.SetEvent;
end
else
Result := DefWindowProc(FWindowHandle, Msg, wParam, lParam);
end;
end;
望高手指教!