简体   繁体   中英

How to stop background/worker thread and start again?

I have an Activity class which has a EditText field. Every time a user enters more than 2 letters i start a service using StartService() which search through a list of country names. This service starts a background/worker thread using -

AsyncTask backgoundTask = new AsyncTask;
backgoundTask.execute(givenString);

And in the doInBackground() function of AsyncTask class i start searching in a list of country names which contains those two letters.

protected Void doInBackground(String[] objects) {
                Log.i("doInBackground()",Thread.currentThread().getName());
                //code to check if country name contains those two/more letters 
                return null;
}

Issue i am having is- Suppose i type two letter and the service starts which starts searching in the list of country names. Then i type the 3rd letter and it hits the service again which again search in the list of country names while the other on in progress. The searches does not run simultaneously. first the search goes on for 2 letters input and then it goes on for 3 letters input.

What i need is- if a search is already going on, then stop that search and start again with new set of letters. For example- if search is already going on for 2 letters input, when user enters 3rd letter, stop the previous search with 2 letters and start with 3 letters.

I tried- calling stopService() first when input is more than 2 letters and then startService(). But its did not work. I can see in Logcat that search went through the full list of country names once and then again for the 3 letters.

I am using started service. I am thinking of using intent service. Any suggestion greatly appreciated. thank you.

*********-- Update --************

Thank all of you for suggesting backgroundtask.cancel(). this is what i did -

@Override
        protected void onCancelled() {
            super.onCancelled();
            Log.i("onCancelled()",Thread.currentThread().getName());
            //findHost(objects[0]);
        }


        @Override
        protected Void doInBackground(String[] objects) {
            Log.i("doInBackground()",Thread.currentThread().getName());
            if(backgoundTask.isCancelled()){
                findCountry(objects[0]);
            }else{
                backgoundTask.cancel(true);
                Log.i("Running()","Background task running again");
                findCountry(objects[0]);
            }

            return null;
        }

But the service is still searching country name twice or as many time as user input is more than 2 letters.

What I'd do is use AutoCompleteTextView, check out link below:

https://developer.android.com/reference/android/widget/AutoCompleteTextView.html

Though you are trying to stop your service, the async task is still alive on the thread. You need to make sure backgoundTask.cancel(true) gets called as well, possibly in the onStop() method of your service.

Always use cancel() to stop a AsyncTask before starting the new task:

backgoundTask.cancel(true);

The documentation of AsyncTask provide details in the 'Cancelling a task' section:

A task can be cancelled at any time by invoking cancel(boolean) . Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object) , instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns. To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]) , if possible (inside a loop for instance.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM