While the loop has not completed

I am developing a java project where an external microcontroller device is connected via a serial port to a computer. I have a wait dialog in a java application waiting for a command to execute on a microcontroller. If the microcontroller does not respond within 10 seconds, the application is turned off.

The problem is that some commands run too fast and the microcontroller responds before the wait dialog is activated. This causes the application to shut down after ten seconds.

Here is the first idea that I decided to wait for the dialog box to appear:

new Thread() {
    public void run() {
         while (!Main.mainFrame.waitDialog.isVisible()) {}
         Main.usbManager.start();
    }
}.start();

But the application gets stuck in the while loop and the wait dialog is displayed, but if I add some random sentence to the while loop, for example System.out.flush();, it works, and when the dialog is visible, the program will exit the while loop.

How can I wait for the dialog to be visible?

thank

+4
source share
2 answers

Most graphic libraries are single-threaded. This means that calls to it may be unsafe, you will never see changes. In particular, values booleancan be embedded, so after a certain point you can be sure that you will not see a change in value.

-, , ( ) , System.out.flush(), .

Thread.yield() Thread.sleep(40), .

+5

, .isVisible() while.

- while, System.out.flush();,

" while (true) - Java" while

, System.out.println()

, -, , .

+3

All Articles