C Creideiki Active Member Licensed User Longtime User Aug 31, 2020 #1 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 Code: Copy 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: Code: Copy 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: Code: Copy 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 MiniTest.zip 9.1 KB · Views: 516
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 Code: Copy 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: Code: Copy 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: Code: Copy 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?
C Creideiki Active Member Licensed User Longtime User Aug 31, 2020 #2 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
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...
Erel B4X founder Staff member Licensed User Longtime User Aug 31, 2020 #3 There is a bug here and it does look related to that thread. I will check it. Upvote 0
Erel B4X founder Staff member Licensed User Longtime User Aug 31, 2020 #4 Note that adding this line to each of the classes does solve this issue: Code: Copy Private stub As Button 'ignore It means that you can't declare jobList in Process_Globals. I recommend you to switch to B4XPages. It will make things simpler. Upvote 0
Note that adding this line to each of the classes does solve this issue: Code: Copy Private stub As Button 'ignore It means that you can't declare jobList in Process_Globals. I recommend you to switch to B4XPages. It will make things simpler.
C Creideiki Active Member Licensed User Longtime User Sep 1, 2020 #5 Erel said: Note that adding this line to each of the classes does solve this issue: Code: Copy Private stub As Button 'ignore It means that you can't declare jobList in Process_Globals. Click to expand... I'll have to try. I recommend you to switch to B4XPages. It will make things simpler. Click to expand... 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 said: Note that adding this line to each of the classes does solve this issue: Code: Copy Private stub As Button 'ignore It means that you can't declare jobList in Process_Globals. Click to expand... I'll have to try. I recommend you to switch to B4XPages. It will make things simpler. Click to expand... 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?
Erel B4X founder Staff member Licensed User Longtime User Sep 1, 2020 #6 Creideiki said: 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... Click to expand... I agree. Creideiki said: Do you have a guess when this bug will be fixed? Click to expand... 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
Creideiki said: 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... Click to expand... I agree. Creideiki said: Do you have a guess when this bug will be fixed? Click to expand... 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.