B4J Tutorial [server] Background Workers

jServer v2.70 adds support for background workers (https://www.b4x.com/android/forum/threads/updates-to-internal-libaries.48274/#post-465493).

A background worker is a class instance that is initialized by a background thread. You can keep the class instance running for as long as the server is running with code similar to:
B4X:
'Class: SomeTask
Sub Class_Globals
   Private timer1 As Timer   
End Sub

Public Sub Initialize
   timer1.Initialize("timer1", 10 * DateTime.TicksPerMinute)
   timer1.Enabled = True
   StartMessageLoop '<- don't forget!
End Sub

Sub Timer1_Tick
   'do the work required
End Sub
You need to tell the server to create the worker by calling Server.AddBackgroundWorker:
B4X:
srvr.AddBackgroundWorker("SomeTask")

You can use it for tasks that should run periodically and are too heavy to be executed by the main thread.
For example you can use it to fetch data from another source and add it to the database.

Note that in debug mode the code will be executed by the main thread.
 
Top