B4J Question Wait for thread to finish

wl

Well-Known Member
Licensed User
Hi,

I have implemented a network protocol using AsyncStreamsText. This if course works asynchroneously.
However the communication is called from a console append I want the main thread to wait until the async protocol handling has finished.

What I tried was:
- the main thread starts the Async communication and the calls StartMessageLoop
- the Async thread/class sets a class variable "result as boolean" and has its async handling of events but when the communication ends it calls sets "results = true" and then calls StopMessageLoop
- the next statement in the main thread (after StartMessageLoop) checks for the variable "result"

In debug mode at least it seems I have a race condition because I receive a message that a string cannot be converted in a Boolean (caused by reading the result). The console app (in debug mode) also seems to stop randomly after this.

What would be the appropriate way (I would like to keep the async handling but keep the async handling outside my main thread / console app).

Thanks
 

Erel

B4X founder
Staff member
Licensed User
There is no such thing as "async thread". Assuming that this is not a server project, all your code runs in the same thread. This also means that there cannot be a race condition.

The solution should be to use StartMessageLoop and StopMessageLoop. Can you post the full error message and the relevant code? Does it also happen in release mode?
 
Upvote 0

wl

Well-Known Member
Licensed User
ok, I understand

However: the problem is in fact during DEBUGGING. The application just quits.

Meanwhile I modified my code to deal with the async way: which meant I needed to split the logic in different callback methods (and having a global variable) passing info from one method to a callback method. Really ugly ...
 
Upvote 0
Top