Android Question Error with threading library

maxh2815

Member
I am currently trying to get a project working which uses the threading library created by agraham. I am using this code to start the thread:
B4X:
Sub Button1_Click
    Dim params(0) As Object
    thread1.Start(Null, "threadSub", params)
End Sub

Sub threadSub
    For i = 0 To 20000
        If i Mod 2000 = 0 Then
            Log(i)
        End If
    Next
End Sub

Sub thread1_Ended(endedOK As Boolean, error As String)
    If endedOK = True Then
        Log("Thread ended OK")
    Else
        Log("Thread did not end OK.")
        Log(error)
    End If
End Sub
However, it instantly fails and logs the output
Logs:
Thread did not end OK.
Exception : Wrong number of arguments; expected 4, got 0
This code is similar to the demo code and I cannot see where I have gone wrong. I then ran the unmodified demo code, and found that it throws the same error. Maybe this library is outdated and no longer works? Or am I just doing something wrong? If there is another newer threading library then I would appreciate a link. Thanks.
 

DonManfred

Expert
Licensed User
Longtime User
In most cases it is a mistake to use this lib.

What are you trying to archieve? Probably there are better ways.
 
Upvote 0

maxh2815

Member
I have an array of integers. For every integer, the program needs to wait for that exact amount of time, and then run some special code. The first thing I tried was using a for loop, and every loop would use the Sleep() function for a delay before running the special code. However, that didnt work very well because Sleep doesnt have very good accuracy (Sleep(20) will sleep for at least 40ms in reality). I need the accuracy/granularity to be to the millisecond, preferably better if possible.

The next thing I tried was using timers. For every integer in the array, I would set the timer interval to the value of the integer and when the timer fired/ticked, it would run the special code. This option also doesnt work as the timer suffers from the same inaccuracy as the Sleep function.

After that, I thought I would do away with inbuilt things entirely, and make my own system. I was going to use some sort of infinite while loop that would check against system time with Datetime.Now, which would be far more accurate. Though when I have a while loop it obviously wont let the main thread refresh and it would freeze up. I cant use Sleep to solve this problem, because even Sleep(0) will sleep for at least 20ms longer than it is supposed to. So, I though that if I ran this loop on a different thread, it wouldnt lock up the main thread and everything would be fine.
 
Upvote 0
D

Deleted member 103

Guest
I am currently trying to get a project working which uses the threading library created by agraham. I am using this code to start the thread:
B4X:
Sub Button1_Click
    Dim params(0) As Object
    thread1.Start(Null, "threadSub", params)
End Sub

Sub threadSub
    For i = 0 To 20000
        If i Mod 2000 = 0 Then
            Log(i)
        End If
    Next
End Sub

Sub thread1_Ended(endedOK As Boolean, error As String)
    If endedOK = True Then
        Log("Thread ended OK")
    Else
        Log("Thread did not end OK.")
        Log(error)
    End If
End Sub
However, it instantly fails and logs the output
Logs:
Thread did not end OK.
Exception : Wrong number of arguments; expected 4, got 0
This code is similar to the demo code and I cannot see where I have gone wrong. I then ran the unmodified demo code, and found that it throws the same error. Maybe this library is outdated and no longer works? Or am I just doing something wrong? If there is another newer threading library then I would appreciate a link. Thanks.
This is how it should work:
B4X:
thread1.Start(Me, "threadSub", params)
 
Upvote 0

maxh2815

Member
That does not affect the error unfortunately. Also, in the xml docs of the lib he mentions that you don't need to pass Me to thread.Start() in activities or services because their subs are static methods.
For a Sub declared in a Class an object instance is required on which to invoke the thread Sub.
This can be Me if Start is called within a class or a reference to the class instance if Start
is called outside the Class.

For Subs declared in Activities and Services, which are actually static methods, an instance
is not required and null can be passed.
All of my code is contained within an activity.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
This works fine for me
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    Private thread1 As Thread
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    If FirstTime Then
        thread1.Initialise("thread1")
    End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    Dim params(0) As Object
    thread1.Start(Null, "threadSub", params)
End Sub

Sub threadSub
    For i = 0 To 20000
        If i Mod 2000 = 0 Then
            Log(i)
        End If
    Next
End Sub

Sub thread1_Ended(endedOK As Boolean, error As String)
    If endedOK = True Then
        Log("Thread ended OK")
    Else
        Log("Thread did not end OK.")
        Log(error)
    End If
End Sub
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
0
2000
4000
6000
8000
10000
12000
14000
16000
18000
20000
Thread ended OK
 
Upvote 0
Top