Android Question Wait For with CallSub doesn't work

Creideiki

Active Member
Licensed User
Longtime User
Hi,

I want to do some Jobs on start of an App. Since there are user interactions and I have to handle with MsgBoxAsync and Wait For, there are Services to start and so on, it's quite difficult to keep the order of the jobs.
Especially because anything runs in Activity_Create, and some jobs should run in Activity_Resume, but only after the other jobs are done...
So I try to put anything in little classes and created a JobListClass, which handles them.
In Main I have
Main:
Sub Process_Globals
    Private jobList As JobListClass
End Sub
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout1")

    Log("Test startet...")
    
    jobList.Initialize
    Dim job As JobClass
    job.Initialize
    jobList.addJob(job, "First Job")

    jobList.runJobs
End Sub

The Jobs only have to have a Sub called runJob:
JobClass:
Public Sub runJob As ResumableSub
    Log("runJob: started")
    Dim sf As Object = Msgbox2Async("Hello World.", "Info", "OK", "", "", Null, False)
    Wait For (sf) MsgBox_Result (ret As Int)
    Log("runJob: ready.")
    Return 0
End Sub

The JobListClass isn't very complicated, too:
JobListClass:
Sub Class_Globals
    Private JobList As List
    Private Running As Boolean
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    JobList.Initialize
    Running = False
End Sub

' Füge einen Job der Liste zu
Public Sub addJob(Job As Object, text As String)
    If SubExists(Job, "runJob") Then
        JobList.Add(Job)
        Log("JobListClass: " & text & " added.")
    Else
        Log("JobListClass: " & text & " has no Sub runJob!")
    End If
End Sub

' Jobs laufen lassen
Public Sub runJobs
    If Running Then
        Log("JobList.runJobs: jobs are already running")
        Return
    End If
    Running = True
    ' Die Jobs sollen im Hintergrund laufen
    Sleep(0)
    Do While JobList.Size > 0
        Dim job As Object = JobList.Get(0)
        JobList.RemoveAt(0)
        Log("runJobs: starting job...")
        Wait For (CallSub(job, "runJob")) Complete (dummy As Object)
        Log("runJobs: job ended. Jobs left: " & JobList.Size)
    Loop
    Running = False
End Sub

Well, the whole thing runs until the "Wait For (CallSub(Job, ..." in the JobListClass. The job itself starts and ends, but the Wait For never seems to fire the Complete event.

Why?
 

Attachments

  • MiniTest.zip
    9.1 KB · Views: 173

Creideiki

Active Member
Licensed User
Longtime User
Hm, well... I actually can answer my own question.
After reading this thread, I removed the sleep(0) and now it works. Using the class in Activity context doesn't work here.

I however still don't know, why that happens...
 
Upvote 0

Creideiki

Active Member
Licensed User
Longtime User
Note that adding this line to each of the classes does solve this issue:
B4X:
Private stub As Button 'ignore
It means that you can't declare jobList in Process_Globals.
I'll have to try.
I recommend you to switch to B4XPages. It will make things simpler.
Well... to start a new project, it'll be worth a try.
But I have > 50 modules with 16 activities and about 20000 lines of code in this project... I don't think, I want to rewrite the whole App...

Do you have a guess when this bug will be fixed?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
But I have > 50 modules with 16 activities and about 20000 lines of code in this project... I don't think, I want to rewrite the whole App...
I agree.

Do you have a guess when this bug will be fixed?
It will take some time. Don't wait for it.
If you can't use the above workaround then move the declaration to the starter service. I think that it will also work.
 
Upvote 0
Top