Can a service end itself early

barx

Well-Known Member
Licensed User
Longtime User
Basically I want a service to check if a database file exists. If it does then do something (including reading said file). If not then kill itself as it cannot do what it needs to without it. Is it just a matter of a 'return' like this .

B4X:
   If File.ExternalWritable Then
      If File.Exists(File.DirDefaultExternal, DatabaseName) Then
         DatabaseFileDir = File.DirDefaultExternal
      End If
   Else
      If File.Exists(File.DirInternal, DatabaseName) Then
         DatabaseFileDir = File.DirInternal
      Else
         Return 'Die
      End If
   End If
   
   SQL.Initialize(DatabaseFileDir, DatabaseName, False)

Also can a service read/write globals declared in another code module if the service has started at boot but the app itself hasn't been run yet?

Thinking that once the service has found the db that it could set the vars in the code module so it can be later read by the app when run so they are both definately using same file. Although, literally as i have just written that down thought, I guess I could have the vars declared in the service modules process globals and then activity to read them from there. That would work wouldn't it lol.

Thanks
 

thedesolatesoul

Expert
Licensed User
Longtime User
Basically I want a service to check if a database file exists. If it does then do something (including reading said file). If not then kill itself as it cannot do what it needs to without it. Is it just a matter of a 'return' like this .
You can do StopService("") to stop the service from within. I like to call it an inward collapse ;)

Also can a service read/write globals declared in another code module if the service has started at boot but the app itself hasn't been run yet?
Yes, it can. But do you think they will hold their values there?

Thinking that once the service has found the db that it could set the vars in the code module so it can be later read by the app when run so they are both definately using same file. Although, literally as i have just written that down thought, I guess I could have the vars declared in the service modules process globals and then activity to read them from there. That would work wouldn't it lol.
The variables in the service module will be destroyed once the service is destroyed. Same is true for the activity. The variables in the code module will only retain value when either activity or service is active.
If the activity and service are completely independent, then I think you will have to write the values to file when exiting from the service/activity.

In the activity I usually check,
B4X:
If IsPaused(service) then
   'Read from file
Else
   'Use Varialbes

End If
(credit to corwin42 for teaching me this)
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Thanks alot, deffinately gives me something to work with ;)
 
Upvote 0
Top