TSocketThread = class(TThread)
private
FWindow: THandle;
protected
procedure WindowMethod(var Message: TMessage);
public
constructor Create
destructor Destroy; override;
end;
// create thread
constructor TSocketThread.Create;
begin
inherited Create(True);
FWindow := AllocateHWnd(WindowMethod); // 安装消息截获函数
// execute thread
Resume;
end;
// destroy thread
destructor TSocketThread.Destroy;
begin
DeallocateHWnd(FWindow); // 释放消息截获句柄
...
inherited Destroy;
end;
// 响应Windows消息
procedure TSocketThread.WindowMethod(var Message: TMessage);
begin
with Message do
case Msg of
FD_ACCEPT: ....;
FD_CLOSE:...;
FD_READ:....;
else
Result := DefWindowProc(FWindow, Msg, wParam, lParam);
end;
end;
end;
用TSocketThread 的FWindows设置消息投递即可:
WSAAsyncSelect(ListenSocket,FWindow,WM_SOCKET_UMC,FD_ACCEPT or FD_CLOSE or FD_READ);