Создаю подключение и Qery в потоке но поток зависает и при переходе с одной вкладки на другую считывание останавливается.
Код:
procedure TClientThread.Execute;
begin
inherited;
with MainForm do
begin
TCPClient[z] :=TIdTCPClient.Create(Mainform);
TCPClient[z].Host :=labelIP0.Caption;
TCPClient[z].Port := StrToInt(LabelPort0.caption);
TCPClient[z].Connect(5000);
Memo0.Enabled := True; Memo0.Color := clWindow;
MainForm.AddLogWrite('Connect ...... ');
MainForm.RzGlyphStatus4.ImageIndex:=3;
Led0.Brush.Color := clBlue;
ZQ[z]:=tZQuery.Create(MainForm);
if not TCPClient[z].Connected then
exit;
repeat
cmd.text:= TCPClient[z].ReadLn();
if trim(cmd.text) <> '' then
begin
if VerificaComando(cmd.text,'msg=',true) then
Synchronize(ShowReceiveMsg)
else if VerificaComando(cmd.text,'server_error=',true) then
Synchronize(ShowServerError)
else Synchronize(UnknowCmd);
end;
until not IdTCPClient1.Connected;
end;
if not Terminated then
Terminate;
end;
procedure TMainForm.UnknowCmd;
var str: String;
S, S1,d1: String;
s3:real;
a,b:integer;
i1,i2: Integer;
r,m : double;
i:integer;
JpegIm: TJpegImage;
bm: TBitMap;
d3,d2: TDateTime;
begin
try
repeat
str:=cmd.text;
if str<>'' then
begin
Memo0.Lines.Add(cmd.text);
s1:=only_numbers(str);
s1:=StringReplace(s1, '.',',' , [rfReplaceAll]);
i1:=Pos(',',s1);
if i1>0 then begin
i2:=PosEx(',',s1,i1+1);
if i2>0 then s1:=Copy(s1,1,i2-1)+Copy(s1,i2+1,Length(s1)-i2);
end;
r:=RoundTo(StrToFloatDef(s1,0),-2);
Edit0.Text:=Floattostr(r);
if (r > 0) AND (r <> last_massa) then
begin
edtNotes0.Text:=edtNotes0.Text+Edit0.Text +#9+TimeToStr(now) +#13#10;
if strtofloat(Edit0.Text)>strtofloat(OptionForm.RzEdit4.Text) then
begin
ZQ[z].SQL.Clear;
zQ[z].SQL.Add ('INSERT INTO base (Date_posting, Time_posting, num, Massa, Name, Image, Com, BaudRate, Organization, Location, IP, Note, Model, Equopment, Linkey, Stable_weight, Image_kamera)'
+' Values (:date1, :date2, 1, :massa, :name,''image'', :port, :BaudRate, :Organization, :Location, :Ip, :Note, :Model, :Equopment, :Linkey, 0, :adress)');
d3:=Trunc(Date);
d2:=frac(time);
zQ[z].Params.ParamByName('date1').Value:=d3;
zQ[z].Params.ParamByName('date2').Value:=d2;
zQ[z].Params.ParamByName('massa').Value:=Edit0.Text;
zQ[z].Params.ParamByName('name').Value:=labelModel0.Caption+' '+LabelEquopment0.Caption;
zQ[z].Params.ParamByName('port').Value:=labelPort0.Caption;
zQ[z].Params.ParamByName('BaudRate').Value:=LabelBaudRate0.Caption;
zQ[z].Params.ParamByName('Organization').Value:=LabelOrganization0.Caption;
zQ[z].Params.ParamByName('Location').Value:=LabelLocation0.Caption;
zQ[z].Params.ParamByName('Ip').Value:=LabelIp0.Caption;
zQ[z].Params.ParamByName('Note').Value:=labelNote0.Caption;
zQ[z].Params.ParamByName('Model').Value:=labelModel0.Caption;
zQ[z].Params.ParamByName('Equopment').Value:=labelEquopment0.Caption;
zQ[z].Params.ParamByName('Linkey').Value:=labelLinkey0.Caption;
zQ[z].Params.ParamByName('adress').Value:=OptionForm.DirectoryEdit1.Text+'\NO.jpg';
zQ[z].ExecSQL;
last_massa := r;
end;
end;
end;
if Optionform.RzEdit1.Text<>'' then
begin
a:=memo0.Lines.Count;
if a>Strtoint(Optionform.RzEdit1.Text) then
memo0.Clear;
end;
if Optionform.RzEdit2.Text<>'' then
begin
b:=edtNotes0.Lines.Count;
if b>Strtoint(Optionform.RzEdit2.Text) then
edtNotes0.Clear;
end;
until str='';
except
on E:Exception do begin
ShowMessage(E.Message);
end;
end;
procedure TMainForm.Button3Click(Sender: TObject);
var
ClientThread:TClientThread;
begin
if Led0.Brush.Color = clRed then
begin
ClientThread:=TClientThread.Create(true);
ClientThread.FreeOnTerminate:=true;
ClientThread.Priority:=tpLower;
ClientThread.Resume;
Led0.Brush.Color := clBlue;
end
else if Led0.Brush.Color = clBlue then ShowMessage('Процесс уже запущен!');
end;