看看我的多线程??
现在有三个问题>>
1,creat执行完后不会自动执行execute过程..在create中调用一下excute后就会一直在执行鼠标一直忙个不停,
2,怎么才可以停止这个线程...我想在关闭client这个sock的时候也结束这个线程应该怎么写结束..
3,在线程不对窗口的edit,memo的操作,也就是执行F_main.edits[N].text:='...'时没有反应,应该怎么操作??
源码如下:
unit foctem;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables, StdCtrls, ExtCtrls, Buttons, ScktComp, MSWinsockLib_TLB,
OleCtrls,Winsock;
type
clientsock = class(TThread)
private
client:TSocket;
N:integer;
Procedure Timerun;
{ Private declarations }
protected
procedure Execute; override;
Public
constructor Create(FT:Boolean;{port,}IP:string;CN:integer);
end;
{ Public declarations }
end;
var
F_octem: TF_octem;
onconnect :array[0..9] of integer;
T:array[0..9] of integer;
//of_did,ofip:string;
//定义Winsock API调用相关变量
Client : array [0..9] of Tsocket;//Tclientsock;
ca:SOCKADDR_IN;
hostaddr:u_long;
implementation
uses main;
{$R *.DFM}
Procedure Clientsock.Timerun;
const
BlockLen=1024*4;
var
s: array[0..BlockLen-1] of byte;
z:byte;
rm,st,ls_ic:string;
r,i,j,K:integer;
evttime:Tdatetime;
//W1of_did,W1ofip:string;
//X:integer;
begin
{ Place thread code here }
r:=recv(Client,s,BlockLen,0) ; //收到的字节数
case F_octem.T[N] of
8: //更新卡表
begin
if Ft[N]=0 then
begin
if s[0]=38 then //收到握手&
begin
F_main.Edits[N].Text:='握手成功!' ;
Dics[N]:=F_main.SendiCs_a(Lock_ip[0]);
DNoc[N]:=IntToHex(Length(Dics[N]) div 8,2);
Dxors[N]:=F_main.compute_xor(DNoc[N]+Dics[N]);
F_main.Sic_com('G'+DNoc[N]+Dics[N]+Dxors[N],8,0);//判断通信连接正常,发IC卡表,最好写函数
DS_L[N]:=length(DNoc[N]+Dics[N]+Dxors[N]) div 2; //发送位数
send(client,F_main.Dsic[N],DS_L[N]+1,0);//发送卡表信息
Ft[N]:=Ft[N]+1;
end;
end
else
begin
if s[0]=36 then //收到成功信息 $
begin
F_octem.Memo1.Clear ;
F_octem.Memo1.Lines.Add('更新卡表成功');
closeSocket(client);
//Timer1.Enabled :=False;
//初始化
F_main.Client_close(0);
exit;
end
else
if s[0]=37 then //收到重发%
begin
if Ft[N]>2 then
begin
F_octem.Memo1.Clear ;
F_octem.Memo1.Lines.Add('更新卡表失败');
F_main.Memo1.Lines.Add('更新卡表重发三次错误,锁可能故障,请检查锁状态1');
closeSocket(Client);
//Timer1.Enabled:=false;
F_main.Client_Close(0);
exit;
end
else
begin
send(client,F_main.Dsic[N],DS_L[N]+1,0);//发送卡表信息
F_main.Edits[N].Text :='重发IC卡表';
Ft[N]:=Ft[N]+1;
end;
end;
end;
end;
9: //强制开锁
begin
if Ft[N]=0 then
begin
if s[0]=38 then //接收握手信号 &
begin
F_main.Edits[N].Text:='握手成功!' ;
s_time[N]:=FormatDateTime('YYYYMMDDHHMM',now());
xor_time[N]:=F_main.Compute_xor(s_time[N]); //计算时间密钥
forcdN[N]:=F_main.Compute_xor(s_time[N]+'a') ; //计算强开执行码
//Dxors[N]:= Compute_xor(s_time[N]+xor_time[N]+forcdN[N]);
Dxors[N]:=F_main.Compute_xor(forcdN[N]);
F_main.Sic_com('I08'+s_time[N]+xor_time[N]+forcdN[N]+Dxors[N],9,0);
Send(Client,F_main.Dsic[N],11,0); //发送强开指令
F_main.Edits[N].Text :='发送强开指令时间-ws1';
Ft[N]:=Ft[N]+1;
end ;
end
else
begin
if s[0]=36 then //$
begin
Evttime:=F_main.nowtime();
F_main.Insert_ofdevtlog(Lock_id[N],evttime,'1','远程开门');
F_octem.Memo1.Clear ;
F_octem.Memo1.Lines.Add('强开成功!');
closeSocket(Client) ;
//Timer1.Enabled :=False;
F_main.Client_Close(0); //初始化下行变量
free;
Exit;
end
else
begin
if s[0]=37 then //收到重发%
begin
if Ft[N]>2 then //重发三次停止
begin
Evttime:=F_main.nowtime();
F_main.Insert_ofdevtlog(Lock_id[N],evttime,'3','远开失败');
F_octem.Memo1.Clear ;
F_octem.Memo1.Lines.Add('强开失败');
F_main.Memo1.Lines.Add('重发三次错误,锁可能故障,请检查锁状态1');
closeSocket(Client);
//Timer1.Enabled:=false;
F_main.Client_Close(0);
exit;
end
else
begin
Send(Client,F_main.Dsic[N],11,0); //发送强开指令
Ft[N]:=Ft[N]+1;
F_main.Edits[N].Text :='重发强开批令!';
Ft[N]:=Ft[N]+1;
end;
end;
end;
end;
end;
12:
begin //巡检
if Ft[N]=0 then
begin
if s[0]=38 then //收到&
begin
F_main.Edits[N].Text :='巡检握手成功';
s_time[N]:=FormatDateTime('YYYYMMDDHHMM',now());
xor_time[N]:=F_main.Compute_xor(s_time[N]); //计算时间密钥
F_main.Sic_com('L06'+s_time[N]+xor_time[N],12,0);
Send(Client,F_main.Dsic[N],9,0); //发送巡检
Ft[N]:=Ft[N]+1;
end;
end
else
begin
if Ft[N]>3 then
begin
F_main.Memo1.Lines.Add('重发三次错误,锁可能故障,请检查锁状态1');
closeSocket(Client);
//Timer1.Enabled:=false;
F_main.Client_Close(0);
exit;
end
else
begin
//判断接收正确
j:=s[0];
for i:=1 to r-1 do
begin
j:=j xor s[i];
end;
if j<>0 then
begin
z:=37; //%
Send(Client,z,1,0);
Ft[N]:=Ft[N]+1;
end
else
if j =0 then
begin
z:=36; //$
Send(Client,z,1,0);
F_main.Memo1.Lines.Add('锁巡检正确锁号:'+Lock_id[N]);
closeSocket(Client);
//Timer1.Enabled:=false;
F_main.Client_Close(0);
//把Byte转成string
for i:=0 to (r-1) do
begin
if s[i]=10 then st:=st+'a'
else
if s[i]=11 then st:=st+'b'
else
if s[i]=12 then st:=st+'c'
else
if s[i]=13 then st:=st+'d'
else
if s[i]=14 then st:=st+'e'
else
if s[i]=15 then st:=st+'f'
else
st:=st+IntTostr(s[i]);
end;
//DNoc[N]:=copy(st,1,2); //记录个数
//Dxors[N]:=copy(st,r-1,2);
for k:=1 to (r-4)div 20 do
begin
st:=copy(st,3+(k-1)*20,20);
evttime:=StrToDatetime(copy(st,1,4)+'-'+copy(st,5,2)+'-'+copy(st,7,2)+' '+copy(st,9,2)+':'+copy(st,11,2));
ls_ic:=copy(st,13,8);
F_main.Insert_ofdevtlog(Lock_id[N],evttime,'1',ls_ic);
end;
exit;
end;
end;
end;
end;
end;
//end;
end;