-3

I am calling an asynctask from the main thread of my Android application. However, I need to pause the main thread till the time the async task is over. Can someone suggest how to do this?

Edit : I need to use an AsyncTask, as I can not perform network requests on the main thread

7
  • 1
    If you want to do that then why are you using an AsyncTask? The point of an AsyncTask is to not pause the main thread when doing some work. Also went would you want to do that? Pausing the main thread is something you should avoid. Jun 16, 2016 at 12:02
  • It's a network request Jun 16, 2016 at 12:02
  • 1
    You're answer is a bit strange, you want to start an AsyncTask from main thread and then stop main thread. So why to start another thread?
    – lubilis
    Jun 16, 2016 at 12:03
  • And why would you want to block the main thread while you are performing that network request? Jun 16, 2016 at 12:04
  • Pausing the main thread is a bad thing to do as it may confuse the user about what's happening. Instead add a progress dialog to let the user know that something is going on. Jun 16, 2016 at 12:04

4 Answers 4

1

Just add Progress Dialog.

ProgressDialog progress = new ProgressDialog(this);
progress.setMessage("please wait ");

show it from

    onPreExecute(){
      progress.show();
    }

dismiss on

onPostExecute(){
   progress.dismiss(); 
 }
2
  • This won't stop main thread, this will lock user interaction on screen. Maybe the question is bad and this is the real answer
    – lubilis
    Jun 16, 2016 at 12:05
  • actually stoping main thread is what just dont allow user to do anything
    – Gaurav
    Jun 16, 2016 at 12:08
1

One way is to show a dialog until your AsyncTask executes and dismiss the dialog in postExecute()

 public class MyAsyncTask extends AsyncTask<Void, Void, Void> {

        ProgressDialog dialog;



        @Override
        protected void onPreExecute() {
            super.onPreExecute();
 dialog = new ProgressDialog(UserMessages.this);
            dialog.setIndeterminate(true);
            dialog.setMessage("Please Wait...");
            dialog.setTitle("Loading Messages");
            dialog.setCancelable(true);
            dialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {

                     // Do your work

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
 dialog.dismiss();
        }


        }
1

You cannot pause the main thread but you can wait until AsyncTask complete and generate it's result by using

AsyncTask.get()

this method will fetch the result you returning in the DoInBackground(), once the task finishes then it will implement the further code

for example if you returning string then write your method like this,

String result=AsyncTask.get();
-1

You can make network requests in Android using volley library instead of async task. It creates a thread pool and reduces the load on the main thread. https://developer.android.com/training/volley/index.html

1
  • 1
    Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. From How to Answer. Jun 16, 2016 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.