The code you showed is the right way to deal with the problem of reconnecting, but there is one small change you need to make. Modify the except block to catch an Exception or EIdException instead of an EIdSocketError . EIdSocketError is not the only possible exception that Connect() can raise. For example, it can raise an EIdConnectException or EIdConnectTimeout , none of which are derived from an EIdSocketError .
procedure SendingThread.Execute; var ... IsConnected : Boolean; begin ... IsConnected := False; while (not IsConnected) and (not Terminated) do begin try TcpClient.Connect; IsConnected := True; except on E: Exception do IsConnected := False; end; end; ... end;
You can also just delete the on E sentence as a whole, as it does nothing useful. The IsConnected variable already has the value False when an exception occurs, so there is no need to reassign it with the same value.
procedure SendingThread.Execute; var ... IsConnected : Boolean; begin ... IsConnected := false; while (not IsConnected) and (not Terminated) do begin try TcpClient.Connect; IsConnected := True; except end; end; ... end;
source share