Android Question Threading library problem.

LucianoB

Member
Licensed User
Longtime User
Good morning everyone.
I rarely post here because I usually try to solve my coding problems by browsing through the forum and the web, but this time I can’t figure it out.
I have an issue with the Threading library (Ver.1.10) when starting a thread. I’ve posted the problematic code, but if needed, I can provide the complete app.

The code
B4X:
Sub Globals
    Dim Socket As JavaObject
    Dim arr() As String
    Dim t As Thread
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("timeguardian1")
    t.Initialise("ConnectToServer_Task") ' Initializes thread
    arr = Array As String("192.168.1.50","12345") 'IP address of PC, open port of PC
End Sub

Sub ConnectToServerAsync()
    Log("log1")
    If t.IsInitialized Then Log("is initialized")
    t.Start(Me, "ConnectToServer_Task", arr)    '   !!!!This instruction does not start the sub ConnectToServer_Task!!!!
    Log("log2")
End Sub

Sub ConnectToServer_Task()
    Log("log3")
    Dim jo As JavaObject
    Try
        jo.InitializeNewInstance("java.net.Socket", Array As Object(arr))
        Log("Connection successful!")
        CallSubDelayed2(Me, "ConnectionEstablished", jo)
    Catch
        Log("Error during connection: " & LastException.Message)
    End Try
End Sub

The istruction at line 16
B4X:
 t.Start(Me, "ConnectToServer_Task", arr)
does not start the sub ConnectToServer_Task at all. I have tried both in Release mode and Debug mode—nothing works. Moreover, the debugger doesn’t report any error. Stepping through with F8 in debug mode, the debugger simply skips over it and continues with the next instruction, without invoking the mentioned sub.

Any advice? Thank you.

BTW.
I'm using B4A v.13.00
Device with Android v.11
 

Sagenut

Expert
Licensed User
Longtime User
In the code that you posted the sub
B4X:
Sub ConnectToServerAsync()
    Log("log1")
    If t.IsInitialized Then Log("is initialized")
    t.Start(Me, "ConnectToServer_Task", arr)    '   !!!!This instruction does not start the sub ConnectToServer_Task!!!!
    Log("log2")
End Sub
it's never called.
 
Upvote 0

LucianoB

Member
Licensed User
Longtime User
Well, my fault, I didn't post the calling button code. This is the all code.
B4X:
Sub Process_Globals
End Sub

Sub Globals
    Dim Socket As JavaObject
    Dim arr() As String
    Dim t As Thread
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("timeguardian1")
    t.Initialise("ConnectToServer_Task") ' Initializes thread
    arr = Array As String("192.168.1.50","12345") 'IP address of PC, open port of PC
End Sub

Sub ButtonStop_Click
    Log("Sending STOP command...")
    ConnectToServerAsync
End Sub

Sub ConnectToServerAsync()
    Log("log1")
    If t.IsInitialized Then Log("is initialized")
    t.Start(Me, "ConnectToServer_Task", arr)    '   !!!!This instruction does not start the sub ConnectToServer_Task!!!!
    Log("log2")
End Sub

Sub ConnectToServer_Task()
    Log("log3")
    Dim jo As JavaObject
    Try
        jo.InitializeNewInstance("java.net.Socket", Array As Object(arr))
        Log("Connection successful!")
        CallSubDelayed2(Me, "ConnectionEstablished", jo)
    Catch
        Log("Error during connection: " & LastException.Message)
    End Try
End Sub

Sub ConnectionEstablished(jo As JavaObject)
    Socket = jo
    Log("Connection established, ready to send commands.")
    SendCommand("STOP")
End Sub

Sub SendCommand(command As String)
    If Socket.IsInitialized Then
        Try
            Dim outputStream As JavaObject = Socket.RunMethod("getOutputStream", Null)
            outputStream.RunMethod("write", Array As Object(command.GetBytes("UTF-8")))
            outputStream.RunMethod("flush", Null)
            Log("Command sent successfully: " & command)
        Catch
            Log("Error while sending command: " & LastException.Message)
        End Try
    Else
        Log("Error: the connection is not initialized.")
    End If
End Sub

Sub CloseConnection
    If Socket.IsInitialized Then
        Try
            Socket.RunMethod("close", Null)
            Log("Connection closed.")
        Catch
            Log("Error while closing the connection: " & LastException.Message)
        End Try
    End If
End Sub

It just goes through line 24 without calling it.
To Erel: on the other side I have a pc which receives the command STOP. It doesn't receive anything...
I'd like to be sure this code is correct.

The logs are the following:
"Sending STOP command..."
"log1"
"is initialized"
"log2"

"log3" was never shown.


And thanks for your replies, guys!
 
Upvote 0
Top