Bug? Resumable Subs that return a value make code run in main thread instead of owner thread

mindful

Active Member
Licensed User
I do not know if this is a bug or I need to handle things differently, but I have a problem. I have a Background Worker where I have some resumable subs.

I have attached a reproducer. Better to look at it as it hard to explain the problem.

I need all the code to run on the BackgroundWorker Thread, but as you can see if you run the project code after the Resumable Subs that return a value is running on the main thread instead of the worker thread.

B4X:
2018-02-09 18:41:38.045:INFO::main: Logging initialized @227ms to org.eclipse.jetty.util.log.StdErrLog
2018-02-09 18:41:38.302:INFO:oejs.Server:main: jetty-9.4.z-SNAPSHOT
2018-02-09 18:41:38.396:INFO:oejs.session:main: DefaultSessionIdManager workerName=node0
2018-02-09 18:41:38.396:INFO:oejs.session:main: No SessionScavenger set, using defaults
2018-02-09 18:41:38.399:INFO:oejs.session:main: Scavenging every 600000ms
2018-02-09 18:41:38.405:INFO:oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@64616ca2{/,file:///C:/WORK/DEV/B4J/TestNestedWaitFor/Objects/www,AVAILABLE}
2018-02-09 18:41:38.412:INFO:oejs.AbstractNCSARequestLog:main: Opened C:\WORK\DEV\B4J\TestNestedWaitFor\Objects\logs\b4j-2018_02_09.request.log
2018-02-09 18:41:38.526:INFO:oejs.AbstractConnector:main: Started ServerConnector@52af6cff{HTTP/1.1,[http/1.1]}{0.0.0.0:51042}
2018-02-09 18:41:38.526:INFO:oejs.Server:main: Started @710ms
1 Current Thread: Thread-9
Registered
2 Current Thread: Thread-9
3 Current Thread: Thread-9
4 Current Thread: Thread-9
7 Current Thread: Thread-9
5 Current Thread: Thread-9
8 Current Thread: Thread-9
9 Current Thread: Thread-9
6 Current Thread: Thread-9
13 Current Thread: Thread-9
10 Current Thread: main
11 Current Thread: main
14 Current Thread: Thread-9
12 Current Thread: main
9 Current Thread: main
13 Current Thread: Thread-9
10 Current Thread: main
11 Current Thread: main
14 Current Thread: Thread-9
12 Current Thread: main
 

Attachments

  • TestNestedWaitFor.zip
    1.9 KB · Views: 264

mindful

Active Member
Licensed User
My reproducer is more "complex" as is an small example how my code is structured, but I just realised that any code that is after Wait For (rs) Complete .... will be executed by the main thread instead of the owner thread.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've tested it with this code:
B4X:
Public Sub Initialize
   Log("1 Current Thread: " & t.InitializeStatic("java.lang.Thread").RunMethodJO("currentThread", Null).RunMethod("getName", Null))
   Main.RegisterBGW(Me)
   Log("2 Current Thread: " & t.InitializeStatic("java.lang.Thread").RunMethodJO("currentThread", Null).RunMethod("getName", Null))
   StartMessageLoop
End Sub

Public Sub Start()
   Log("3 Current Thread: " & t.InitializeStatic("java.lang.Thread").RunMethodJO("currentThread", Null).RunMethod("getName", Null))
   Sleep(100)
   Log("4 Current Thread: " & t.InitializeStatic("java.lang.Thread").RunMethodJO("currentThread", Null).RunMethod("getName", Null))
End Sub
It seems to work properly. The output is:

Registered
1 Current Thread: Thread-9
2 Current Thread: Thread-9
3 Current Thread: Thread-9
4 Current Thread: Thread-9

Make sure to test it in release mode.
 

mindful

Active Member
Licensed User
Yes your code works, but the problem is with resumable sub that return a value.

B4X:
Some code ' this runs in the worker thread
Wait for (rs) Complete (Result as Boolean) ' this also runs in the worker thread
If Result Then ' this code runs in the main thread and all the rest of the code from this sub
....

As you can see in my reproducer i've used resumable sub that return something, not simple resumable subs.
 
Top