看看我的多线程??

aliyan 2002-03-14 04:46:54
现在有三个问题>>
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;

...全文
45 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
aliyan 2002-03-14
  • 打赏
  • 举报
回复
再接:
Function TF_Octem.sock_YN(Tt:integer;sock_ip:string;ofdids:string):String; //
begin
if onconnect[0]=0 then
begin
T[0]:=Tt;
lock_ip[0]:=Sock_ip;
Lock_id[0]:=ofdids;
//F_main.Timer1.Enabled:=True;
F_main.Edits[0].Text :='操作锁'+Lock_id[0];
clientsock.Create(True,Sock_ip,0) ;
onconnect[0]:=1;
result:='1';
end
else
if onconnect[1]=0 then
begin
T[1]:=Tt;
lock_ip[1]:=Sock_ip;
Lock_id[1]:=ofdids;
F_main.Edits[1].Text :='操作锁'+Lock_id[1];
clientsock.Create(True,Sock_ip,1) ;

onconnect[1]:=1;
result:='2';

end
else
begin
result:='0';
end;
end;
..
//执行
procedure TF_octem.BitBtn1Click(Sender: TObject);
var
j,k:Integer;
of_did,ofip,r:string;
begin
if RadioButton5.Checked then //12巡检
begin
memo1.Clear ;
memo1.Lines.Add('开始执行巡检!');
listBox1.Clear ;
//取出IP地址
try
with Query1 do
begin
close;
Sql.Clear;
Sql.Add('select ofdid,ofip,ofstate from ofdoors where ofstate=''1''');
open;
end;
while not Query1.Eof do
begin
of_did:=Query1.FieldByName('ofdid').AsString;
ofip:=Query1.FieldByName('ofip').AsString;
listbox1.Items.Add(ofip+' '+of_did);
Query1.Next ; //判断是否有连接如果没有连接则还执行本次循环
end;
except
showmessage('对不起,执行巡检时取IP地址错误!');
Abort;
end;

for j:=0 to listBox1.Items.Count-1 do
begin
of_did:=copy(Listbox1.Items[j],length(Listbox1.Items[j])-7,8);
ofip:=trim(copy(Listbox1.Items[j],1,length(Listbox1.Items[j])-8));
r:=Sock_YN(12,ofip,of_did); //12是巡检
if r='0' then //如果没有空闲通道时还有IP
begin
For k:=0 to j-1 do
begin
Listbox1.Items.Delete(0);
end;
Break;
end
else
begin
if j=listBox1.Items.Count-1 then //如果一次通道够用或者全连不通
For k:=0 to j do
begin
Listbox1.Items.Delete(0);
end;
end;
end;
Timer1.Enabled :=True; //启动巡检检测器判断是否结束
end
else
begin
if (Trim(Edit2.Text)='') or (length(Edit2.Text)<8) then
begin
showmessage('对不起,没有输入房门编码或者输入的房门编码小于8');
Abort;
end
else
if (not RadioButton1.Checked) and (not RadioButton2.Checked) and
(not RadioButton3.Checked) and (not RadioButton4.Checked) then
begin
showmessage('对不起,没有选择功能操作,请选择!');
Abort;
end
else
begin
of_did:=Trim(Edit2.Text); //取得房门编码
ofip:=Select_ofip(of_did); //取得该锁号的IP lock_ip[1]
if ofip='' then
begin
showmessage('对不起,锁号为:'+ofdid[1]+'的IP为空,请进入管理中心进行IP添加!');
Abort;
end
else
begin
if RadioButton1.Checked then //8更新卡表
begin
r:=Sock_YN(8,ofip,of_did) ; //判断Winsock状态建立连接 //更新卡表8
case StrToInt(r) of
0:
begin
memo1.Clear ;
memo1.Lines.Add('没有空闲通道');
end;
else
begin
memo1.Clear ;
memo1.Lines.Add('开始更新卡表');
end ;
end;
end
else
if RadioButton2.Checked then //9强制开锁
begin
memo1.Clear ;
memo1.Lines.Add('开始强制开锁');
r:=Sock_YN(9,ofip,of_did); //判断Winsock状态建立连接
end
end;
end;
end;
end;
aliyan 2002-03-14
  • 打赏
  • 举报
回复
接上:
procedure clientsock.Execute;
begin
while not Terminated do
begin
Synchronize(Timerun);
sleep(100);
end;
end;

constructor ClientSock.Create(FT:Boolean;{port,}IP:string;CN:integer);
var
s:byte;
begin
inherited Create(FT);
Client:=Socket(PF_INET,SOCK_STREAM,IPPROTO_IP);
if Client=INVALID_SOCKET then //创建错误
begin
F_main.Memo1.Lines.Add('为连接锁创建Socket错误!');
Exit;
end
else
begin
//定义协议族
ca.sin_family:=PF_INET; //tcp
//将端口号由主机字节顺序转换为网络字节顺序;调用htons
ca.sin_port:=htons(StrToint('1024')); //端口号
//调用inet_addr函数将点分制十进制IP地址转换成网络地址,
//此过程隐含地实现了到网络字节的转换;
Hostaddr:=inet_addr(PChar(trim(ip))); //ip地址
if hostaddr=-1 then
begin
F_main.Memo1.Lines.Add('主机IP地址'+ip+'错误');
closeSocket(Client);
Exit;
end
else
begin
//转换成功对地址赋值
ca.sin_addr.S_addr:=hostaddr;
//连接由协议族、地址、端口三元组确定了的服务器端;
if connect(Client,ca,sizeof(ca))<>0 then //连接失败
begin
F_main.Memo1.Lines.Add('连接锁错误,锁IP:'+IP);
CloseSocket(Client);
exit;
end
else
begin
s:=38; //&
Send(Client,s,1,0);
F_main.Edits[N].Text :='发送握手信号';
end;
end;
end;

N:=CN;
FreeOnTerminate:=True;
//Synchronize(Timerun);
Execute;
end;

830

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧