0

I have an activity which makes periodic requests (once every 15 seconds to get a json data feed). The requests I am passing off to a AsyncTask so its not on the main UI thread. So far so good. But lets say that I request the feed and it takes 20 seconds to respond. I really don't want to kick off another thread until say 30 seconds are up. So ....

Is there a way to prevent the AsyncTask from running if there first one has not yet finished?

Also is there a way to timebox the AsyncTask to take no more than 30 seconds? reguardless of the Http timeout?

Thanks, Steve

Code Complete
  • 43
  • 1
  • 5
  • Duplicated questions: http://stackoverflow.com/questions/11667064/create-a-timer-to-send-http-request-periodically-android http://stackoverflow.com/questions/7045300/doing-http-request-from-android-using-timer Personally, I'd propose to use `Handler` and link it to itself – keaukraine Nov 22 '12 at 08:24

1 Answers1

0

You can use a mutex variable to enforce this. Say, We call the variable update. This will be a boolean variable with initial value true.

Before running an Async task, Check the value of update, If it is true run the async task and set update to false.

After the end of the async task, in the onPostExecute() method, set update to true again.

So another async task will not be launched while one hasn't finished, thus ensuring exclusivity.

Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84