Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TIdTCPClient on Android: Can't catch Exception Popup - Socket Error #107
#5
(06-19-2020, 03:36 PM)Peter_WB Wrote: The IdTCPClient.Connect was done inside a TThread to prevent delays in the main task.
For android only I run the Connect instruction in Synchronized(FClient.Connect).

That is a really bad idea.

TThread.Synchronize() calls the specified procedure in the main UI thread. TIdTCPClient.Connect() is a synchronous method that blocks the thread that calls it. So, you would be blocking your app's main UI thread, and iOS and Android are not very forgiving of that. If your app blocks the main UI thread for a noticeable duration, those OSes are very likely to just outright kill your app process immediately. So don't EVER block the main UI thread on a mobile platform. Doing that is not a good idea even on desktop platforms either, but at least those OSes are more forgiving of slow UIs. But users are not.

Not to mention, Android does not allow network operations in the main UI thread anyway. It will raise a NetworkOnMainThreadException Java error if you try. The only reason you are not getting that error in this situation is because TIdTCPClient.Connect() internally uses its own worker thread to establish the TCP connection when the TIdTCPClient.ConnectTimeout property is set to a non-infinite value, or the TIdAntiFreeze component is being used. TIdTCPClient.Connect() may be updated in the future to get rid of that internal worker thread, thus the TCP connection would be established in the same thread that calls TIdTCPClient.Connect(), which will then lead to the NetworkOnMainThreadException error on Android if called in the main UI thread.

So, just don't use Synchronized(FClient.Connect) at all, it is not worth the risks.

(06-19-2020, 03:36 PM)Peter_WB Wrote: Since that, the Socket Error 107 is disapperared, only the EIdSocketError is called.

Then the problem is not related to Indy at all. You have now proved that, when your debugging saw the EIdSocketError exception raised by TIdTCPClient.Connect() being caught in your except block. So, the popup message is a completely different issue that is unrelated to TIdTCPClient.Connect() itself. Either there is a flaw in how Delphi's RTL handles exceptions in worker threads on Android, or the popup is occurring inside of your own ReplyMessage() which you are calling inside of your except block (I'm leaning towards the latter).

(06-19-2020, 03:36 PM)Peter_WB Wrote: Are there options to update the Indy code that the connect is operable even when called from a thread ?

TIdTCPClient.Connect() itself already works fine in a worker thread. Your debugging has proved that. Something else is going on, and you will have to debug further to find out what exactly. If the popup is not occurring inside of ReplyMessage(), then it has to be inside the RTL itself, in which case you will have to debug into that source code to find it, and then report the culprit to Embarcadero.

Reply


Messages In This Thread
RE: TIdTCPClient on Android: Can't catch Exception Popup - Socket Error #107 - by rlebeau - 06-19-2020, 06:11 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)