Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Apr : tidclient disconnect close problem

www.cryer.info
Managed Newsgroup Archive

tidclient disconnect close problem

Subject:tidclient disconnect close problem
Posted by:"mustafa korkmaz" (no..@none.com)
Date:15 Apr 2007 04:30:55

There is only one problem.
if the connection closed by server program "connect again" button appears on the user screen.
if the user dont use this button and try to close the program the program can not be closed or freeze.
It can be closed only task manager.
while the program conected to server if the program has not been closed by server it can be closed properly.

How should be my TcpClientDisconnected event code?
Because when the connection be disconnected user can connect again with one button.

Is there any wrong line below?


TClientThread = class(TThread)
private
  procedure MyOnTerminate( sender: TObject );
....
end;


constructor TClientThread.Create( ww: boolean );
begin
Onterminate := MyOnterminate;
inherited Create( false );
end;


procedure TClientThread.MyOnTerminate( sender: TObject );
begin
  if frmMainform.TcpClient.Connected then
  try
   frmMainform.Tcpclient.disconnect;
  except
  end;
end;


procedure TClientThread.Execute;
begin
frmMainform.TcpClient.ReadTimeout := 17000;
  while ( not Terminated ) do
  begin
      try
        frmMainform.TcpClient.ReadBuffer( firstbyte, SizeOf ( firstbyte ) );
        if firstbyte = 34 then....
        else
        if firstbyte = 36 then....
        else
        ........
      except on e: exception do
      begin
       Terminate;
      end;
      end;
  end;
end;
--------------------------------------------------------------------------------------

procedure TfrmMainform.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
       if ClientThread <> nil then ClientThread.Terminate;
        try
            TCPClient.Disconnect;
        finally
            if ClientThread <> nil then
            begin
                ClientThread.WaitFor;
                FreeAndNil(ClientThread);
            end;
        end;
end;


procedure TfrmMainform.MyConnect;
begin
    TcpClient.Connect( 20000 );
    ClientThread := TClientThread.Create( false );
........
end;


procedure TfrmMainform.TcpClientDisconnected(Sender: TObject);
begin
waittimer1.enabled := true;
//if I dont use timer the program freeze on "ClientThread.WaitFor();" line.
end;


procedure TfrmMainform.waittimer1Timer(Sender: TObject);
begin
waittimer1.enabled := false;
  if ClientThread <> nil then
  begin
     ClientThread.Terminate;
     try
       try
         frmMainform.TcpClient.Disconnect;
       except
       end;
     finally
       if ClientThread <> nil then
       begin
         ClientThread.WaitFor();
         FreeAndNil( ClientThread );
       end;
     end;
  end;
end;

Replies:

www.cryer.info
Managed Newsgroup Archive