B4J Library ABBackgroundWorkers (extracted from jServer)

This library is just an extraction of the Background Worker functionality in the jServer library. I needed this functionality but did not want to include the whole Jetty part. Could be useful for someone.

Lib source code Github: https://github.com/RealAlwaysbusy/ABBackgroundWorkers

Usage:

Main
B4X:
Sub Process_Globals
    Dim bgw As ABBackgroundWorkers
   
End Sub

Sub AppStart (Args() As String)
    bgw.Initialize
   
    bgw.AddBackgroundWorker("Worker1")
    bgw.AddBackgroundWorker("Worker2")
   
    bgw.Start
   
    StartMessageLoop
End Sub

Worker1
B4X:
Sub Class_Globals
    Private Timer As Timer
    Private TimerTickMs As Int = 10000
End Sub

Public Sub Initialize
    Timer.Initialize("Timer", TimerTickMs)
    Timer.Enabled = True
   
    StartMessageLoop '<- don't forget!
End Sub

Sub Timer_Tick
    'do the work required
    Log("Worker 1: every " & TimerTickMs)
End Sub

Worker2
B4X:
Sub Class_Globals
    Private Timer As Timer
    Private TimerTickMs As Int = 5000
End Sub

Public Sub Initialize
    Timer.Initialize("Timer", TimerTickMs)
    Timer.Enabled = True
   
    StartMessageLoop '<- don't forget!
End Sub

Sub Timer_Tick
    'do the work required
    Log("Worker 2: every " & TimerTickMs)
End Sub

Alwaysbusy
 

Attachments

  • ABBackgroundWorkers.zip
    6.6 KB · Views: 222
  • TestBGWorkers.zip
    1.6 KB · Views: 235
Last edited:

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
This is awesome thankyou !!
Can it be used with ui?? I guess as long as i don't call properties between each thread it should be fine... right??
 

incendio

Well-Known Member
Licensed User
Longtime User
Thanks for this library.

I didn't see stop method. How do I stop background process when all job done?
 
Top