Wait for inline thread to complete before moving to next method

10,206

Solution 1

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.

Solution 2

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

}

Solution 3

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(); 
}
Share:
10,206
Tyler
Author by

Tyler

Updated on August 21, 2022

Comments

  • Tyler
    Tyler over 1 year

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

    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();           
            }
        }.start(); 
    
        stepTwo();
    }
    

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

    Thanks!