B4J Question How to access class properties in a background worker?

Diceman

Active Member
Licensed User
If I am running a server and I have a background worker (which is a class) running, how can I read properties, modify properties and run methods of this class from other modules?

TIA

Sample Code:
B4X:
'--- Main code ---
Sub Process_Globals
 Public EmailMsgs1 As clsEmailMsgs
 ...
End Sub

Sub AppStart (Args() As String)
    ...
    srvr.AddBackgroundWorker("clsEmailMsgs") 
    StartMessageLoop
end sub


'--- clsEmailMsgs Code ---
Sub Initialize
 Main.EmailMsgs1 = Me
End Sub

'--- Module1 Code ---
if Main.EmailMsgs1.Running=False then    'How can I rewrite these lines using "Wait For"?
  Main.EmailMsgs1.Running = true
  Main.EMailMsgs1.StartEmailing
end if
 

Gunther

Active Member
Licensed User
Longtime User
If you have the class code then you can use public for the procedures and variable which you want to expose to external code modules.

You can create a callback to your modul which raises the event you need to react on it.

See the training session of class modules for further reading.
 
Last edited:
Upvote 0

Diceman

Active Member
Licensed User
Hi Gunther,
I've worked a lot with classes, but this is a special case. It is a background worker class. To access the background worker, because it is on a different thread, the recommended method is:

According to Erel: "Now you can use CallSubDelayed (Gate4Dimmer1, "SomeSub") to call a sub from the Main or any other module."
See https://www.b4x.com/android/forum/threads/hot-get-backgroundworker-from-main-bas.86013/
I want to know if it is possible to use "Wait For" instead of "CallSubDelayed" to access properties and methods in the background thread. This will make the code more readable because I can retrieve values from the background class properties.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
If you are accessing primitive or string variables then you don't need to do anything special. Just get them directly.

Wait For and CallSubDelayed are not interchangeable. If you want to call a sub in the worker class instance then you need to use CallSubDelayed. You can then call Wait For and wait for an "event" that is raised from the worker class with CallSubDelayed.
 
Upvote 0
Top