B4J Question Background workers question

Robert Valentino

Well-Known Member
Licensed User
Longtime User
in this example: https://www.b4x.com/android/forum/threads/solved-background-workers-passing-parameters.80898/#content

the code shows:

Server Code is setting a Main Variable

B4X:
Public Sub Initialize
   T.Initialize("T",5000)
   If Not(Main.BG0.IsInitialized) Then
     Main.BG0=Me
     LogMe("bg0")
   else if Not(Main.BG1.IsInitialized) Then
     Main.BG1=Me
     LogMe("bg1")
   End If
   T.Enabled=True
   StartMessageLoop '<- don't forget!
End Sub

Main Code is waiting for Server to set Variable and is calling Class routines directly.

B4X:
Sub Process_Globals
   Public srvr As Server
   Public BG0 As SomeTask
End Sub

Sub AppStart (Args() As String)
   srvr.Initialize("")
   srvr.Port=8081
   srvr.AddBackgroundWorker("SomeTask")
   srvr.Start
   bg0Task
   StartMessageLoop   
End Sub

Sub bg0Task
     If Not(BG0.IsInitialized) Then
       Sleep(500)
       CallSubDelayed(Me,"bg0Task")
     Else
       BG0.SetName("bg0")
       Log($"bg0GetName: ${BG0.GetName}"$)
     End If
End Sub


I've been looking at all the samples and just want to make sure this is the proper way to interface the server to the main.

My server is going to be running in the background handling Directory request (load a list of directories, delete a list of files...)


I know this is may be just a coding style but why would you do:
"if Not(BG0.IsInitialized)" rather than
"if BG0.IsInitialize = false" is there a trade off I am missing???

Anyway based on this example this is some what I have done
B4X:
Sub Process_Globals
   Private fx                    As JFX
   Private MainForm                As Form

   Public  mDirectoryServer           As cDirectoryServer
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   
   MainForm      = Form1
   MainForm.Title    =   "MP3 Directory Merge"
   
   MainForm.RootPane.LoadLayout("sMain") 'Load the layout file.
   MainForm.Show

   mStopMerge = True
   
   mServer.Initialize("")
   mServer.AddBackgroundWorker("cDirectoryServer")       '<--- would be nice if AddBackgroundWork returned the Class Initialized

   mServer.Start
   
   DirectoryServerAlive
End Sub

Public  Sub DirectoryServerAlive
     Log("DirectoryServerAlive")
     
     If  mDirectoryServer.IsInitialized = False Then  '<---- do not like that server Initialized this class entry
       Log("Not Ready Yet")
       
       Sleep(500)
       
       CallSubDelayed(Me, "DirectoryServerAlive")
       Return
     End If
     

     mDirectoryServer.mCallBack = Me                           
     mDirectoryServer.mEvent  = "ServerCallBack"           
End Sub


B4X:
Sub Class_Globals
   
   Private fx As JFX
   
   Public  mCallBack   As Object
   Public  mEvent     As String
   
   Private mTimer     As Timer    
   
End Sub


Public Sub Initialize
       Log("cDirectoryServer - Initialize")

       mWorkToDo.Initialize
   
       mTimer.Initialize("Timer", 1000)
         mTimer.Enabled = True
       
       If  Main.mDirectoryServer.IsInitialized = False Then     '  <----- really do not like touching the Main from server is there a better way
         Main.mDirectoryServer = Me                                 '  <----- Again is there a better way
       End If
End Sub

Private Sub Timer_Tick
         If  mEvent.Length > 0 Then
         If  SubExists(mCallBack, mEvent)  Then
           mTimer.Enabled = False
           
           CallSubDelayed2(mCallBack, mEvent, "cDirectoryServer Ready")
         End If
       End If
End Sub

Once I get passed the point where I have mEvent an mCallback set then I have the Server and Main talk thru CallSubDelays

Just looking for some pointers so I get off on the right foot.

Thanks

BobVal
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
"if Not(BG0.IsInitialized)" rather than
"if BG0.IsInitialize = false" is there a trade off I am missing???
There is no difference between the two.

mServer.AddBackgroundWorker("cDirectoryServer") '<--- would be nice if AddBackgroundWork returned the Class Initialized
The class instance is only created when you start the server.

Why do you need to pass the EventName and Callback object? This is not a library, you can just use Main and any event name you like. It will simplify your code.
 
Upvote 0
Top