Android Question wrong number of arguments when starting a thread

RichyK68

Active Member
Licensed User
Longtime User
Hi,

I am trying to start a thread from my service like this:

Dim t As Thread
t.Initialise("") ' there are no events in a thread apparently
Dim success As Boolean = t.Start(Null, "MyThread", Null)
Dim error As String = t.Error

and the MySpeakThread method is defined:

Sub MyThread

Log("MyThread")

End Sub

But I get back in t.Error the message "Exception : wrong number of arguments; expected 3, got 0". Where exactly is it expecting three arguments?

Richard
 

RichyK68

Active Member
Licensed User
Longtime User
Well I've tried a different approach (instead of starting a thread whose method runs in the service). I've created a class which I initialize within the service, then call the class' Start method which does this:


Dim t As Thread
t.Initialise("")
Dim args(1) As Object
args(0) = "test"
Dim success As Boolean = t.Start(Me, "MyThread", Array As Object(1, "java.lang.Object", args)) ' I'm not sure why I even need three parameters here, no documentation on the subject from what I can find, but I cannot Start a thread without an array containing an int, a string, and an object array it keeps telling me

and the MyThread method looks like

Sub MyThread(a as message)

Log("MyThread")

End Sub

Well the thread 'apparently' starts successfully, no errors returned, but the MyThread is not being called and I get this message in the Log:

Unexpected event (missing RaiseSynchronousEvents): java.lang.Object

Using threads surely can't be this difficult, I'm a thread king within .Net, when I first started using threading years ago it took me all of 30 seconds to get my first thread running, Microsoft made it that difficult. Now after several hours I still haven't got a thread started in Android.

I'm sure it's easy when you know how but the documentation is sparse and not very helpful. Can anybody help here?

Thanks in advance,

Richard
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Why do you need to create an additional thread? One of the design concepts behind Basic4android is that the libraries should take care of handling long running tasks with background threads. Except of a few cases you do not need to create and manage background threads yourself.

The debugger doesn't support background threads (in B4A code). So you will need to switch to release mode.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
I had a similar (but different) issue with threads. I was running a UI update on a game board on a separate thread & found that it worked perfectly in debug mode, but after I compiled a release, it seemed to not work at all. I confirmed that the thread was the cause of the issue by replacing the thread code with a direct call to the UI update sub & this fixed the issue. In the end I decided not to use threading for this particular app, so I never got to the bottom of why it worked in debug mode but not in the release.
 
Upvote 0
Top