开发者

Asynch thread stopping main UI thread

开发者 https://www.devze.com 2023-03-01 03:07 出处:网络
Can anyone tell m开发者_如何学运维e 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 wh

Can anyone tell m开发者_如何学运维e 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();


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.


call the start method

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

see this example as well


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

0

精彩评论

暂无评论...
验证码 换一张
取 消