Android Code Snippet Close the socket and stream properly.

This approach prevents another process from attempting to close the socket and the stream at the same time.

B4X:
Private myclient As Socket
Private astream As AsyncStreams
Private IsClosing As Boolean

Public Sub CloseConnection
    If IsClosing Then Return
    IsClosing = True
    If astream.IsInitialized Then astream.Close
    If myclient.IsInitialized Then myclient.Close
    IsClosing = False
End Sub
 

Alessandro71

Well-Known Member
Licensed User
Longtime User
what is the use case: is there any chance of parallel processing in B4A?
i always thought that on Android the process was only one, with no resource contemption occourring.
or were you referring to a server implementation, or using the threading library?
 

Filippo

Expert
Licensed User
Longtime User
what is the use case: is there any chance of parallel processing in B4A?
The AsyncStreams library is asynchronous. This means that the Close command is not executed immediately, so problems can arise if you try to execute the “Close” command from another procedure.

I received a crash report from an app and asked an AI where this might be coming from.

Here is an excerpt from the “crash report”:

My previous code looked like this:
B4X:
Public Sub CloseConnection
    If myclient.IsInitialized Then myclient.Close
    If astream.IsInitialized Then astream.Close
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…