Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if all AsyncTasks have finished

Tags:

I have 3 AsyncTasks and 1 ProgressBar. I want when any of task executes, the progress bar is visible and when all of them finish, the progress bar is invisible.

In Java, there is ExecutorService::isTerminated to check if all runnables finished but Android doesn't have it.

Update: 3 tasks execute at the same time.

Figure. enter image description here

like image 670
emeraldhieu Avatar asked Aug 03 '11 10:08

emeraldhieu


People also ask

How do I know if async task is completed?

You can return a Task or Task<T> and use that to determine if it's completed. Also you can use a CancellationToken and cooperative cancellation to cancel previous tasks. Right now async void is unawawaitable and fire-and-forgot so you won't have any idea if it's done or failed etc.

What is a AsyncTask in Android?

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params , Progress and Result , and 4 steps, called onPreExecute , doInBackground , onProgressUpdate and onPostExecute .

How to call AsyncTask in Android?

To start an AsyncTask the following snippet must be present in the MainActivity class : MyTask myTask = new MyTask(); myTask. execute(); In the above snippet we've used a sample classname that extends AsyncTask and execute method is used to start the background thread.


2 Answers

Nice graphic. But I am afraid there is no build in mechanism for this. You'll have to implement it by yourself. There are few solutions you could use -

  1. Keep a reference to all 3 task. When task finishes check if the other two tasks are finished too, if yes than close the progress dialog if no wait for some other task to finish and check again. Make sure you free the references when you're done.
  2. If you don't want to keep a reference store a counter. When the task finishes, increment the counter and check if it's equal to 3. If all tasks finished and you are done. If you implement this make sure to synchronized the access to the counter.
like image 192
Mojo Risin Avatar answered Sep 29 '22 09:09

Mojo Risin


Try using AsyncTask.getStatus(). This works perfectly fine. Refer below sample code.

 List<AsyncTask<String, String, String>> asyncTasks = new ArrayList<AsyncTask<String, String, String>>();  AsyncTask<String, String, String> asyncTask1 = new uploadTask().execute(string);  AsyncTask<String, String, String> asyncTask2 = new downloadTask().execute(string);  AsyncTask<String, String, String> asyncTask3 = new createTask().execute(string);  asyncTasks.add(asyncTask1);  asyncTasks.add(asyncTask2);  asyncTasks.add(asyncTask3); 

You can later loop the AsyncTaskList and find each of the tasks' status as below.

 for(int i=0;i<asyncTasks.size();i++){       AsyncTask<String, String, String> asyncTaskItem = (AsyncTask<String, String, String>)asyncTasks.get(i);       // getStatus() would return PENDING,RUNNING,FINISHED statuses       String status = asyncTaskItem.getStatus().toString();       //if status is FINISHED for all the 3 async tasks, hide the progressbar  } 
like image 43
Jagadeesh Avatar answered Sep 29 '22 09:09

Jagadeesh