Closing the client stream gracefully

I have problems finding a Delphi 7 Indy9 polling client. I tried adding TEvent with waitforsingleobject and in many other ways to disable gracefully. The error occurs in readln. Usually the error is "EIDConnection ... not connected". I put the watch on him and the thread stopped. but "while" does not overestimate the condition until the connection receives a message from the server, so it simply grinds to readln until it receives the msg message. Therefore, sometimes it is disconnected gracefully, but more often it falls. Is there a way to do this, or did I just try ... except to read and continue ... thanks in advance

 procedure TReadingThread.Execute; begin while not Terminated and FConn.Connected do begin // read msg from server Msg := FConn.ReadLn; Synchronize(ReceiveLine); end; end; 
+4
source share
2 answers

I think you need to add code to handle the Disconnect event. I had a similar problem with what you described, and here is what I did (in this example, tcpServer is an instance of TIdTCPServer):

  procedure TformRemoteControlWnd.tcpServerDisconnect(AContext: TIdContext); (* Connection is disconnected. Be careful, because this also gets called when the app is shutting down while a connection is active, in which case tcpServer may be gone already. *) begin if not Application.Terminated and Assigned(tcpServer) then begin sbarStatus.SimpleText := 'TCP/IP Disconnected'; tcpServer.Tag := 0; // used to prevent rentrancy end; // shut down connection to stop thread from calling OnExecute event try AContext.Connection.Disconnect; except Sleep(0); end; end; 
0
source

I found the answer ... Readln will wait indefinitely until it receives a carriage return. So the thread is in Readln until the server sends a message or the socket is disconnected (which causes a failure). A OnDisconnect was written in the Delphi compiler code in OnDisconnect to catch the error using try...except . So I just have to be careful to clean before disconnecting the connector. I thought I could find a cleaner method. Thanks for the help.

0
source

Source: https://habr.com/ru/post/1381371/


All Articles