Code in Activity_Create not running

thedesolatesoul

Expert
Licensed User
Longtime User
Here is a question I knew I would one day ask:

Here is the code:
Activity.LoadLayout("main")
StartService("dpservice")
DB.ServiceInitialise
DB.Authenticate("xxx","xxx")
Msgbox ("Hello","TY")

Now DB.Authenticate kicks off an HTTP request using the HTTP Utils and a service.
Ofcourse, it probably goes off running code in the service thread. But I am sure it should eventually return one day and execute the MsgBox command? It does not.
Even though the UI is completely active. I can do stuff with listviews etc. Will the Msgbox command not be executed until the service dies and all the threads return back here?
Does that in effect mean I am running everything in the Activity_Create sub?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Services do not run in a different thread. Unless you are using the Threading library, your code always run in the main thread (libraries code can run in other threads though).

See the documentation for StartService: Basic4android Search: startservice
It says that a Msgbox cannot be shown after this call and before the service has started.
I will explain it. When you call StartService a message is sent to the message queue. Once this message is processed the service starts. If the service is not started after 20 seconds, Android will force close your application. Therefore it is not possible to show a Msgbox at this time. You can change the order of the statements and first show the Msgbox and then call StartService.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Services do not run in a different thread. Unless you are using the Threading library, your code always run in the main thread (libraries code can run in other threads though).

See the documentation for StartService: Basic4android Search: startservice
It says that a Msgbox cannot be shown after this call and before the service has started.
I will explain it. When you call StartService a message is sent to the message queue. Once this message is processed the service starts. If the service is not started after 20 seconds, Android will force close your application. Therefore it is not possible to show a Msgbox at this time. You can change the order of the statements and first show the Msgbox and then call StartService.
I have to say the B4A can handle this, then the code must be amazing.
Because this is pretty much out of order processing. Even though StartService is called, the MsgBox statement may get to the processor before the Service is actually started. How do you actually prevent the Msgbox from being displayed? :eek:
Well that is B4A internals, and pretty clever.

..so just to get my head around this...
When we kick off StartService, the main thread doesnt just jump off to the service.
It will keep processing (as long as there is nothing modal) until the end of the sub. (If there are long operations in there that take more than 20 secs we will get ANR).
When the sub has finished, and at some point when the service message reaches the end of the que, the service code is actually started here? Or does it start the the previous sub ends and there is nothing to process for the main thread?

I am just asking because it helps in debugging to know what order instructions are executed in.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Whenever the main thread is not busy with executing your code it handles the messages queue. So when it finishes executing your current code it will handle the message queue and eventually start the service.

Makes sense.
Thank you very much! :sign0188:
 
Upvote 0
Top