各位Delphi同志:这段代码的作用能否简述一下。 ( 积分: 100 )

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

luhuaijun

Unregistered / Unconfirmed
GUEST, unregistred user!
unit UMsgClientThread;

interface

uses
ComObj, ActiveX, AxCtrls, Classes,SConnect,
ScktComp,dialogs,SysUtils,winsock,windows,messages;

type
TMsgClientThread = class(TThread)
private
FSocket : TClientSocket;
FUserPanelHandle : HWND;

FErrInfo : String;

function GetWaitEvent: THandle; stdcall;
protected
procedure Execute; override;
public
FEvent : THandle;
constructor Create(CreateSuspended: Boolean;icSocket : TClientSocket);
destructor Destroy; override;

property UserPanelHandle : HWND write FUserPanelHandle;
end;

implementation

uses UMsgSelfDeclare;

{ TMsgClientThread }

constructor TMsgClientThread.Create(CreateSuspended: Boolean;
icSocket : TClientSocket);
begin
Inherited Create(True);
FSocket := icSocket;
self.FreeOnTerminate := true;
end;

destructor TMsgClientThread.Destroy;
begin
inherited;
end;

procedure TMsgClientThread.Execute;
var
msg: TMsg;
lEvent: THandle;
WaitTime : integer;
begin
CoInitialize(nil);
try
PeekMessage(msg, 0, WM_USER, WM_USER, PM_NOREMOVE);
lEvent := self.GetWaitEvent;
FEvent :=lEvent;
WaitTime := INFINITE ;
try
while not Terminated and (FSocket.Socket<> nil) and (FSocket.Socket.Connected) do begin
try
case MsgWaitForMultipleObjects(1, lEvent, False, WaitTime, QS_ALLEVENTS) of
WAIT_OBJECT_0:
begin
WSAResetEvent(lEvent);
Sleep(1);
//通知主线程有数据到
//showmessage('rev');
PostMessage(FUserPanelHandle,WM_MSG_MESSAGE,0,0);
end;
WAIT_OBJECT_0 +1:
begin
while PeekMessage(msg, 0, 0, 0, PM_REMOVE) do
DispatchMessage(msg);
end;
end; // end of case
except
end;
end;//end of while
Except
end;
finally
CoUninitialize();
end;
end;

function TMsgClientThread.GetWaitEvent: THandle;
var
lEvent : THandle;
begin
lEvent := WSACreateEvent;
WSAEventSelect(FSocket.Socket.SocketHandle, lEvent, FD_READ or FD_CLOSE ); //
Result := lEvent;
end;


initialization
LoadWinSock2();

end.
 
unit UMsgClientThread;

interface

uses
ComObj, ActiveX, AxCtrls, Classes,SConnect,
ScktComp,dialogs,SysUtils,winsock,windows,messages;

type
TMsgClientThread = class(TThread)
private
FSocket : TClientSocket;
FUserPanelHandle : HWND;

FErrInfo : String;

function GetWaitEvent: THandle; stdcall;
protected
procedure Execute; override;
public
FEvent : THandle;
constructor Create(CreateSuspended: Boolean;icSocket : TClientSocket);
destructor Destroy; override;

property UserPanelHandle : HWND write FUserPanelHandle;
end;

implementation

uses UMsgSelfDeclare;

{ TMsgClientThread }

constructor TMsgClientThread.Create(CreateSuspended: Boolean;
icSocket : TClientSocket);
begin
Inherited Create(True);
FSocket := icSocket;
self.FreeOnTerminate := true;
end;

destructor TMsgClientThread.Destroy;
begin
inherited;
end;

procedure TMsgClientThread.Execute;
var
msg: TMsg;
lEvent: THandle;
WaitTime : integer;
begin
CoInitialize(nil);
try
PeekMessage(msg, 0, WM_USER, WM_USER, PM_NOREMOVE);
lEvent := self.GetWaitEvent;
FEvent :=lEvent;
WaitTime := INFINITE ;
try
while not Terminated and (FSocket.Socket<> nil) and (FSocket.Socket.Connected) do begin
try
case MsgWaitForMultipleObjects(1, lEvent, False, WaitTime, QS_ALLEVENTS) of
WAIT_OBJECT_0:
begin
WSAResetEvent(lEvent);
Sleep(1);
//通知主线程有数据到
//showmessage('rev');
PostMessage(FUserPanelHandle,WM_MSG_MESSAGE,0,0);
end;
WAIT_OBJECT_0 +1:
begin
while PeekMessage(msg, 0, 0, 0, PM_REMOVE) do
DispatchMessage(msg);
end;
end; // end of case
except
end;
end;//end of while
Except
end;
finally
CoUninitialize();
end;
end;

function TMsgClientThread.GetWaitEvent: THandle;
var
lEvent : THandle;
begin
lEvent := WSACreateEvent;
WSAEventSelect(FSocket.Socket.SocketHandle, lEvent, FD_READ or FD_CLOSE ); //
Result := lEvent;
end;


initialization
LoadWinSock2();

end.
 
后退
顶部