Fabien Biller
Fabien Biller

Reputation: 183

While loop is running in a thread, yet it blocks the main gui

Why does this code block the main UI in the while loop?

new Thread(new Runnable() {
    public void run() {
        someButton.post(new Runnable() {
            public void run() {
                while (HintergrundDienst.laeuft)
                {
                    //some delay code, like Thread.sleep
                }
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        //do on ui after HintergrundDienst.laeuft = false
                    }
                });
            }
        });
    }
}).start();

Running this blocks the main ui.

Upvotes: 0

Views: 29

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006869

Why does this code block the main UI in the while loop?

Because it is running on the main application thread. Your loop is in a Runnable that you are providing to post(). The documentation for post() indicates that "The runnable will be run on the user interface thread". Here, "the user interface thread" refers to the the main application thread.

Upvotes: 5

Related Questions