Hi everybody,
is there a way to wait for a service initialization completed from a service or activity? I need to be sure that the service is completelly started before going on. I do with a sleep(5000), but there is something like a semaphore to be sure of service started ok? The service interfaces a webservice so I cannot be sure about init times.
Sub Activity_Create(FirstTime As Boolean)
CallSubDelayed2(Service1, "StartServiceAndWait", Me)
Wait For Service_Ready
Log("Service1 is ready at this point...")
End Sub
Code in the service:
B4X:
Public Sub StartServiceAndWait(Callback As Object)
CallSubDelayed(Callback, "Service_Ready")
End Sub
Sub Activity_Create(FirstTime As Boolean)
CallSubDelayed2(Service1, "StartServiceAndWait", Me)
Wait For Service_Ready
Log("Service1 is ready at this point...")
End Sub
Code in the service:
B4X:
Public Sub StartServiceAndWait(Callback As Object)
CallSubDelayed(Callback, "Service_Ready")
End Sub
I really have to study the new feature (resumable routines), since I would never have thought of such a solution.
I would have called a callback routine from the Service_Start event.
In fact, I think I did this before, complicating the code (but I was forced to do so, the new feature did not exist).
1. The advantage of CallSubDelayed is that we can pass a parameter (callback module in this case).
2. Instead of handling the Service_Ready event in a different sub we use the new Wait For keyword to catch the event in the current sub.
I'm not sure that starting a service whould have suspended the actual one. One day I'll check! Probably the execution will go on till the next return, if you dont't use CallSub(and have already your service started)
Maybe I explained badly; it is better to use an "old style" example (2 services?):
B4X:
' FirstService
Sub Service_Start
StartService(SecondService)
' I would like to continue here, after SecondService has completed its initialization,
' but without the resumable routines this was not possible.
End Sub
Sub ContinueHere
End Sub
'---------------
' SecondService
Sub Service_Start
CallSubDelayed(FirstService, "ContinueHere")
End Sub
So your code cannot continue immediately after starting SecondService but the flow "jumps" to a different routine, ContinueHere.