VCL不支持线程同步,可以在线程里用同步函数,或用postMessage发送消信给主线程处
理。
同步函数很简单,
PostMessage要在窗口上拦截消息。
如:
const
WM_SOKCETREAD = WM_USER + $F02; //数据到来
WM_TransWorking = WM_USER + $F03; //传输文件工作消息
WM_AddTransFile = WM_USER + $F04; //添加一个传输文件消息
WM_TransTimeOut = WM_USER + $F05; //传输文件超时消息
WM_TransDelete = WM_USER + $F06; //删除一个传输文件消息
WM_TransComplete = WM_USER + $F07; //一个传输文件完成消息
WM_BufferUpFile = WM_USER + $F08; //一个上传文件缓冲完成消息
procedure SocketReadData(var Message: TMessage); message WM_SOKCETREAD;
procedure Tfrm_ClientCtrlPanel.ReadReply(Socket: TCustomSocket;
AData: TStream; var AReply: TStream; var DataType: TSocketDataType);
var
l_Cmd, A_Cmd: word;
l_data: string;
Data: TStream;
begin
if AData.Size = 0 then Exit;
try
//取得命令头
AData.Read(l_Cmd, SizeOf(l_Cmd));
case l_Cmd of
CPP_StartCaptureScr: begin
GetCaptureScreen(AData);
end;
MSG_TEST: begin
Self.UpdateLog('MSG_TEST');
end;
CPP_LOGOUT: ;
MSG_DATA:
begin
Data := TMemoryStream.Create;
Data.CopyFrom(AData, AData.Size - AData.Position);
//发送线程同步消息
PostMessage(Handle, WM_SOKCETREAD, 0, Longint(Data));
end;
else UpdateLog(GetConstName(l_Cmd), True);
end;
except
end;
end;
procedure Tfrm_ClientCtrlPanel.SocketReadData(var Message: TMessage);
begin
if TStream(Message.LParam) <> nil then
DealDataBag(TStream(Message.LParam));
end;