应该在你的线程中构造一个消息循环来进行等待onreader事件的触发
代码如下:
unit threadSocket3;
interface
uses
Classes,ScktComp,sysutils,Graphics,windows,messages,ExtCtrls,forms,decode,Controls,BatchRefresh;
type
tcsocket3 = class(TThread)
private
cskccr:Tclientsocket;
{ Private declarations }
protected
procedure write_canves;
procedure doconnect(Sender: TObject;Socket: TCustomWinSocket);
procedure doError(Sender: TObject;Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
procedure doRead(Sender: TObject;Socket: TCustomWinSocket);
procedure Execute; override;
public
end;
var
CmdStr:string;
DataStr:string;
AddrStr:string;
typeid:integer;
msg:tagMSG;
net_str:string;//网络状态提示字符串;
readerserial:string;//读卡器序列号字符串
time_str:integer;
pcmname:string;
PrintStr:string;
timecount:Ttimer;
k:integer;
function labfaile3(typeid:integer;net_str:string):string;//提示下载失败
function labsucc3(typeid:integer):string;//提示下载成功
procedure Receive_comm3(ReccmdStr:string); //接收命令字符串过程
procedure Receive_data3(RecDataStr:string);//接收数据字符串过程
procedure Recive_addr3(RecAddr:string);//接收IP地址字符串过程
procedure recive_type3(rectype:integer);//接收下载项目类型
procedure recive_name3(reader_name:string);//接收读卡器序名称
procedure recive_pcmid3(pcm_ID:integer);//取得PCM的ID
procedure net_messages3(net_code:integer);//网络状况信息提示;
procedure checkvalue3();
procedure time_count3();//计时器功能,如果超过时间未能接收到返回确认包,即强行结束线程
implementation
procedure checkvalue3();
begin
k:=0;
end;
procedure time_count3();
begin
timecount:=Ttimer.Create(nil);
timecount.Interval:=2;
timecount.Enabled:=true;
k:=k+1;
if (k=2000) and (hid1<>1) then
begin
PostThreadMessage(0,$0012,0,0);
end;
end;
procedure TCsocket3.write_canves;
var
tmp:string;
begin
tmp:=PrintStr;
FrmBatchRefresh.rededit.Lines.Add(tmp);
end;
procedure Receive_comm3(ReccmdStr:string); //接收命令字符串
begin
CmdStr:=ReccmdStr;
end;
procedure Receive_data3(RecDataStr:string);//接收数据字符串
begin
DataStr:=RecDataStr;
end;
procedure Recive_addr3(RecAddr:string);//接收IP地址
begin
AddrStr:=trim(RecAddr);
end;
procedure recive_type3(rectype:integer);//接收下载项目类型
begin
typeid:=rectype;
end;
procedure recive_pcmid3(pcm_id:integer);
begin
pcmname:=inttostr(pcm_id);
end;
procedure recive_name3(reader_name:string);
begin
readerserial:=reader_name;
end;
{连接事件}
procedure TCsocket3.doconnect(Sender: TObject;Socket: TCustomWinSocket);
begin
Socket.SendText(CmdStr);//命令包
Socket.SendText(DataStr);//数据抱
end;
{错误处理事件}
procedure TCsocket3.doError(Sender: TObject;Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
net_messages3(errorcode);
FrmBatchRefresh.rededit.SelAttributes.Color:=clyellow;
PrintStr:=labfaile3(typeid,net_str);
synchronize(write_canves);
errorcode:=0;
PostThreadMessage(0,$0012,0,0);
end;
{读取文本事件}
procedure TCsocket3.doRead(Sender: TObject;Socket: TCustomWinSocket);
var
FStr:string;
begin
FStr:=Socket.ReceiveText;
if Copy(Fstr,1,2)='DT' then
begin
hid1:=1;
PrintStr:=labsucc3(typeid);
FrmBatchRefresh.rededit.SelAttributes.Color:=claqua;
synchronize(write_canves);
PostThreadMessage(0,$0012,0,0);
end;
end;
procedure TCsocket3.Execute;
begin
freeonterminate:=true;
cskCCR:=Tclientsocket.Create(nil);
cskCCR.Address:=AddrStr;
cskCCR.Port:=8001;
cskCCR.ClientType:=ctNonBlocking;
cskCCR.OnConnect:=doconnect;
cskCCR.OnError:=doError;
cskCCR.OnRead :=doread;
cskCCR.Active:=true;
hid1:=0;
checkvalue3();
time_count3();
//__________消息循环________________________
while (GetMessage(msg,0,0,0)) do
begin
dispatchMessage(msg);
end;
//________________________________________
timecount.Enabled:=false;
timecount.Free;
hid:=1; //信号变量,标识线程是否结束0:未结束 1:结束 声明人:王志伟
cskCCR.Close;
cskCCR.Free;
terminate;
end;
function labsucc3(typeid:integer):string;
var
i:integer;
begin
i:=typeid;
case i of
11://门禁机优先序列
begin
result:='向'+pcmname+'号PCM下发'+trim(readerserial)+'的'+'门禁机配置文件成功';
end;
end;
end;
function labfaile3(typeid:integer;net_str:string):string;
var
i:integer;
netstr:string;
begin
i:=typeid;
case i of
11://门禁机优先序列
begin
result:=netstr+' '+'向'+pcmname+'号PCM下发'+trim(readerserial)+'的'+'门禁机配置文件失败';
end;
end;
end;
procedure net_messages3(net_code:integer);
begin
case net_code of
10004:begin
net_str:='操作被终止';
end;
10013:begin
net_str:='访问被拒绝';
end;
10024:begin
net_str:='打开太多的SOCKET';
end;
10048:begin
net_str:='地址已经被使用';
end;
10049:begin
net_str:='设置地址失败';
end;
10050:begin
net_str:='网络关闭';
end;
10051:begin
net_str:='网络不可达';
end;
10052:begin
net_str:='网络被重置';
end;
10055:begin
net_str:='缓冲不足';
end;
10057:begin
net_str:='SOCKET未连接';
end;
10058:begin
net_str:='SOCKET已经关闭';
end;
10060:begin
net_str:='连接超时';
end;
10061:begin
net_str:='连接被拒绝';
end;
10064:begin
net_str:='主机已经关机';
end;
10065:begin
net_str:=' 找不到路由';
end;
10067:begin
net_str:='进程太多';
end;
11001:begin
net_str:='找不到主机';
end
else
net_str:='其他原因导致网络错误';
end;
end;
end.