akupun
akupun

Reputation: 43

Asynch thread stopping main UI thread

Can anyone tell me why the following dialog box does not show until the asynchronous thread has finished. I cannot figure this one out. This is running in the main UI thread. Not sure why a new thread would affect the flow of the main UI thread

                dialog = new ProgressDialog(this);

                dialog.show();

                new Thread(new Runnable() {
                     public void run() {
                         while(imageLoader.isProcessing()) {}
                         doSomething();   
                     }
                 }).run();

Upvotes: 3

Views: 902

Answers (3)

titaniumdecoy
titaniumdecoy

Reputation: 19251

You need to call the start() method of the anonymous Thread, not the run() method.

From the docs:

public void start(): Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

Upvotes: 9

Nirmal- thInk beYond
Nirmal- thInk beYond

Reputation: 12054

call the start method

i reccomend to use use AsyncTask see this , it has proper thread handling mechanism

see this example as well

Upvotes: 2

Dhruv
Dhruv

Reputation: 1129

Don't expect threads to follow the flow of your code. I suggest to use AsyncTask and for showing the dialog you can show the dialog in onPreExecute() and remove it in onPostExecute()

or may be you like to try runOnUiThread()

Upvotes: 0

Related Questions