3

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();
akupun
  • 43
  • 1
  • 3

3 Answers3

9

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.

Stephen C
  • 669,072
  • 92
  • 771
  • 1,162
titaniumdecoy
  • 18,470
  • 17
  • 94
  • 130
2

call the start method

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

see this example as well

Community
  • 1
  • 1
Nirmal- thInk beYond
  • 11,345
  • 8
  • 34
  • 45
0

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()

Dhruv Mevada
  • 1,079
  • 2
  • 13
  • 31