Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for working AsyncTask

I have some work to do in the another thread (by doingAsyncTask). Work start when user click on button. But at the same time only one object of doingAsyncTask must do this work, i meen if doingAsyncTask is working, then click on button must not create a new object of doingAsyncTask and execute it, it must wait until work finish. How can i check it?

public class SearchActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //....
    }

    public void onclickButton(View view) {
        new doingAsyncTask().execute();
    }

    public class doingAsyncTask extends AsyncTask<Void, Void, Void> {
    protected Void doInBackground(Void... unused) {
        //doing something
        return(null);
    }

    protected void onProgressUpdate() {
    }
    protected void onPreExecute() {
    }
    protected void onPostExecute() {
    }
   }
}

SOLVED thx all , its works for me

      if(task.getStatus() == AsyncTask.Status.FINISHED)
            task=new ProgressBarShow();
        if(task.getStatus() == AsyncTask.Status.PENDING){
            //task=new ProgressBarShow();
            task.execute();
        }
like image 222
yital9 Avatar asked Dec 20 '22 23:12

yital9


1 Answers

Check this AsyncTask.Status

AsyncTask.Status    FINISHED    Indicates that onPostExecute(Result) has finished. 
AsyncTask.Status    PENDING     Indicates that the task has not been executed yet. 
AsyncTask.Status    RUNNING     Indicates that the task is running. 

code:

if (doingAsyncTask().getStatus().equals(AsyncTask.Status.FINISHED))
     doingAsyncTask().execute();
else

EDIT:

public class SearchActivity extends Activity {
    doingAsyncTask asyncTask; 

    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);             
       // ...
       asyncTask = new doingAsyncTask();   
    }
    public void onclickButton(View view) {
         if(ayncTask.getStatus().equals(AsyncTask.Status.FINISHED) || ayncTask.getStatus().equals(AsyncTask.Status.PENDING)) {
             asyncTask.execute();
         }
         else {
             // do something
         }
    }
    // ...
}
like image 171
user370305 Avatar answered Jan 07 '23 06:01

user370305