Android Question threading problems (again)

RichyK68

Active Member
Licensed User
Longtime User
Hi,

I'm trying my hand at threading again, this time I'm trying to start a thread from an activity with

Try
Dim t1 As Thread
t1.Initialise("")
t1.Start(Null, "LoadSongList", Array As Object(Null))
Catch
End Try

LoadSongList is defined

Sub LoadSongList(o As Object)
...
End sub

I'm aware that in debug mode threads don't work, but in release mode this just crashes my app and I don't have a clue why. I've had zero luck with threading so far. What am I doing wrong? Should I even be starting a thread from a service? Is there anything wrong with how I am initializing or starting the thread?

Cheers,

Richard
 

RichyK68

Active Member
Licensed User
Longtime User
I've moved the thread object/thread start and the method it should run out of the service and into Main. Now it gives an error message that the sub is not found. I tried the example in the intellisense on the thread object:

Sub AsyncSetMap(m As Object)

End Sub

Dim m As Map
m.Initialize
thread1.Start(Me, "AsyncSetMap", Array As Object(m))

And do you think it works? Noooo, it says it can't find the sub asyncsetmap.

This makes no sense whatsoever...
 
Upvote 0

RichyK68

Active Member
Licensed User
Longtime User
I have some big lists to load in and I don't want to hold up the gui,I want the app to launch as quick as possible. The lists can be loaded as a background thread as they are unrelated to the gui.
 
Upvote 0

RichyK68

Active Member
Licensed User
Longtime User
That would probably be a good option. I'm guessing though that threads don't work particularly well under basic4android? The example given in the inellisense on thread.start makes it look so easy but it simply doesn't work :(
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Threading library should work properly in release mode. Many developers use it.

I always recommend to avoid it if there are simpler alternatives. One of the design concepts behind Basic4android is that libraries should take care of running long tasks with background threads and let the developer work with the main thread without needing to handle all kinds of threading issues.
 
Upvote 0

RichyK68

Active Member
Licensed User
Longtime User
I'd like threading in my development arsenal like it is in .Net for me. Can you give me an example of how to define the method sub and start a thread on it. The example in the intellisense, when wrapped with a try/catch, gives the error message 'sub asyncsetmap not found' (or whatever else I choose to name the sub). This is in release mode. I understand the limitations of the debug mode.
Cheers,
Richard
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The example works fine:
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim t As Thread

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     t.Initialise("t")
     Dim MyMap As Map
     MyMap.Initialize
     MyMap.Put("a", "b")
     t.Start(Me, "Async_SetMap", Array As Object(MyMap))
   End If
End Sub
Private Sub Async_SetMap(MapObj As Object)
   Dim Map1 As Map
   Map1 = MapObj
   'Add some Data to the map
   Map1.Put("A Key", "Some Data")
   Log("Different thread: " & Map1)
End Sub
I've added an underscore to the sub name to avoid problems with the obfuscator.

Note that threads in Android are more complicated compared to desktop apps.
 
Upvote 0

RichyK68

Active Member
Licensed User
Longtime User
Thank you! I'll give this a try in the morning. I'm betting it is the obfuscator that was changing the behaviour of the intellisense example.
 
Upvote 0
Top