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
  • 698,415
  • 94
  • 811
  • 1,216
titaniumdecoy
  • 18,900
  • 17
  • 96
  • 133
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,847
  • 8
  • 35
  • 46
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
  • 1,129
  • 2
  • 13
  • 32