Francesco Galgani
Francesco Galgani

Reputation: 6249

Codename One: wait until a given boolean is true

I need to wait until a given boolean is true. That boolean is updated by a listener. All the code is inside the EDT, so something like the following is not applicable:

while (!myBoolean) {
    Util.sleep(100);
}

How can I stop a flow of execution inside the EDT (in a correct way), until the given boolean becomes true?

Upvotes: 1

Views: 47

Answers (1)

Shai Almog
Shai Almog

Reputation: 52760

The simple answer is:

invokeAndBlock(() -> {
    while (!myBoolean) {
       Util.sleep(100);
    }
});

A better approach would be:

timer = UITimer.schedule(100, true, () -> {
   if(myBoolean) {
      timer.cancel();
      runThisMethod();
   }
});

A more elaborate question is "why not add a listener"?

Since the boolean is changed from a listener why not add a listener after the listener that triggers that and do the work there?

Upvotes: 1

Related Questions