-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

Dhruv Mullick
  • 551
  • 9
  • 25
  • 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. – Xaver Kapeller Jun 16 '16 at 12:02
  • It's a network request – Dhruv Mullick Jun 16 '16 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 '16 at 12:03
  • And why would you want to block the main thread while you are performing that network request? – Xaver Kapeller Jun 16 '16 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. – Prerak Sola Jun 16 '16 at 12:04
  • You should never block the main thread. What you are trying to achieve can be done onPostExecute() – Arpit Ratan Jun 16 '16 at 12:05
  • I plan to redirect to a page which shall show an item which depends on the result of the network request. – Dhruv Mullick Jun 16 '16 at 12:06

4 Answers4

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(); 
 }
Gaurav
  • 3,615
  • 2
  • 27
  • 50
  • 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 '16 at 12:05
  • actually stoping main thread is what just dont allow user to do anything – Gaurav Jun 16 '16 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();
        }


        }
M.Waqas Pervez
  • 2,492
  • 2
  • 19
  • 33
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();
Bhoomit_BB
  • 374
  • 4
  • 10
-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
    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](http://stackoverflow.com/help/how-to-answer). – Gustavo Morales Jun 16 '16 at 12:49