开发者

Wait for inline thread to complete before moving to next method

I have an android app where I am doing the following:

private voi开发者_如何学Cd onCreate() {
    final ProgressDialog dialog = ProgressDialog.show(this, "Please wait..", "Doing stuff..", true);

    new Thread() {
        public void run() {
            //do some serious stuff...
            dialog.dismiss();           
        }
    }.start(); 

    stepTwo();
}

And I would like to ensure that my thread is complete before stepTwo(); is called. How can I do this?

Thanks!


The Thread instance has a join method, so:

private void onCreate() {
    final ProgressDialog dialog = ProgressDialog.show(this, "Please wait..", "Doing stuff..", true);

    Thread t = new Thread() {
        public void run() {
            //do some serious stuff...
            dialog.dismiss();           
        }
    };
    t.start(); 
    t.join();
    stepTwo();

}

You may want to try this though:

private void onCreate() {
    final ProgressDialog dialog = ProgressDialog.show(this, "Please wait..", "Doing stuff..", true);

    Thread t = new Thread() {
        public void run() {
            //do some serious stuff...
            SwingUtilities,invokeLater(new Runnable() {
                public void run() {
                    dialog.dismiss();           
                }
            });
            stepTwo();
        }
    };
    t.start(); 
}

Because onCreate is in the UI thread, having the join in there will freeze the UI till after onCreate completes, saving any dialog till then. stepTwo will have to use SwingUtilities.invokeLater to do any UI changes itself.


If you want to run things in the background I'd recommend using the AsyncTask class as doing so will ensure you interact with the UI thread properly.

Also, if you want code to run after the background task is complete you can just call the method then. There's no reason wait inside onCreate().

Your code will look something like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    new MyAsyncTask().execute();
}

private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
    private ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        dialog = ProgressDialog.show(MyActivity.this, "Please wait..", "Doing stuff..", true);  
    }

    @Override
    protected Void doInBackground(Void... params) {
        //do some serious stuff...
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        dialog.dismiss();
        stepTwo();
    }

}


Another, option is to simply move step2() into the thread so it executes after the thread tasks complete:

private void onCreate() {
    final ProgressDialog dialog = ProgressDialog.show(this, "Please wait..", "Doing stuff..", true);

    new Thread() {
        public void run() {
            //do some serious stuff...
            dialog.dismiss();           
            stepTwo();
        }
    }.start(); 
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜