Timing out an AsyncTask?

by HippoMan » Sat, 10 Apr 2010 01:50:49 GMT

Sponsored Links
 I know how to use AsyncTask in a standard manner to manage operations
that are in the background in relation to a UI thread.

However, I want to run a task in the background which might run for a
very long time under certain circumstances. In these cases, I would
like to force the background task to fail if it runs for an excessive
amount of time.

I know that I can invoke the "get(long timeout, TimeUnit unit)" method
of AsyncTask in my UI thread in order to terminate my background task
if it runs too long. However, in that case, my UI will block while
this "get()" command is waiting.

There are probably other drawbacks to directly calling "get()" in this
manner, not the least of which being an evil interaction with the
"done()" method of AsyncTask's contained FutureTask object, which
itself is calling "get()" -- at least this is what I see when I look
at the source code for AsyncTask.

So what is the canonical, Android-approved method for timing out an
overly-long-running AsyncTask?

Thanks in advance.


Timing out an AsyncTask?

by Romain Guy » Sat, 10 Apr 2010 01:58:07 GMT

 AsyncTask was not designed for long running operations and should
definitely not be used in the way you describe here. To cancel a task,
just invoke cancel().


Sponsored Links

Timing out an AsyncTask?

by HippoMan » Sat, 10 Apr 2010 02:16:38 GMT

 Of course I know about cancel(). When I mentioned the use of "get(long
timeout, TimeUnit unit)", I thought it would be obvious that I would
then invoke cancel() to terminate my overly-long-running task. But
then, my UI thread would block while I'm waiting, as I stated above,
which is not desirable. Nor is the fact that the done() method of
AsyncTask's contained FutureTask object also calls get().

I asked what is the Android-approved, canonical method for doing what
I want to do. Given that AsyncTask is not the intended mechanism for
invoking and optionally timing out a long-running task, what *is* the
recommended method for doing so in Android?

Thanks again, in advance.


Timing out an AsyncTask?

by Romain Guy » Sat, 10 Apr 2010 02:30:05 GMT

 You would be better off just creating your own thread and handling the
timeout inside it.


Timing out an AsyncTask?

by Frank Weiss » Sat, 10 Apr 2010 02:58:40 GMT

 Of the top of my head, set an alarm in the UI Activity to call cancel on the


Timing out an AsyncTask?

by HippoMan » Sat, 10 Apr 2010 02:59:16 GMT

 OK. Thank you.

So how do I interface to my UI from the new thread that I start? Once
the long-running task completes (assuming it finishes before my time-
out period), I want to notify my UI thread so it can take appropriate
action. If this was a short-running task, I could have easily used the
onPostExecute() method of AsyncTask for that purpose.

But when running my own thread, what is the recommended way in Android
to properly communicate the results of the task to the UI thread? I
can can think of several ways to do this, but I'm sure there is a
procedure for this which is consistent with Android "best practices".

Thanks again.


Timing out an AsyncTask?

by social hub » Sat, 10 Apr 2010 03:11:11 GMT

 There is something called Handler through which you can communicate with
your UI thread

hope this helps


Timing out an AsyncTask?

by HippoMan » Sat, 10 Apr 2010 03:15:14 GMT

 OK. I'm going to make a wild guess as to how I might do this. Could
someone comment on the reasonableness of this approach under
Android? ...

1. As part of my Activity class, define a message type called, for
2. As part of my Activity class, define a message handler which
3. Start a new thread. For the purpose of this discussion, call it
4. Inside the run() method of ControllerThread, create a FutureTask
which invokes my long-running process.
5. Instantiate and invoke my FutureTask within ControllerThread and
then wait for its results via "get(long timeout, TimeUnit unit)".
6. If the FutureTask times out, do nothing.
7. If the FutureTask completes properly, send its result back to the
Activity class via a MESSAGE_LONG_RUNNING_TASK_RESULT message.

Does this make sense?

Thanks again.


Timing out an AsyncTask?

by HippoMan » Sat, 10 Apr 2010 03:17:49 GMT

 Thank you, social hub. Our messages crossed. I think that the message
handler I describe in my previous post is the same one that you are
referring to.


Timing out an AsyncTask?

by Sam » Wed, 21 Apr 2010 05:05:25 GMT

 Some things to consider with respect to your proposed approach:

- If the Activity is destroyed while your background threads are
running, what should happen?
    - Should the Activity's onDestroy() cancel the threads somehow?
    - If the threads be allowed to finish their work, what should
happens then?  The original Activity is now dead, so sending a message
to it makes no sense.
- If your ControllerThread (or its Runnable) is a non-static inner
class (which includes anonymous inner classes) of the Activity, it
will hold an implicit reference to the Activity, even after it is
destroyed.  This can be a big memory leak if your thread lasts a long
time.  The same consideration applies to your FutureTask subclass.  In
short, make sure all classes are static or top-level classes.


Other Threads

1. Tanya app untuk hero


Mau nanya app untuk hero :

1. untuk baca file pdb dan prc ?
2. Mobireader untuk android ada ngak?
3. Bisa ngak android on dan off pakai schedule?
4. Gimana hard-reset hero?
5. Bisa ngak kalao mem-card dalam hero diakses langsung oleh pc ? Jadi hero nya 
berfungsi kayak card-reader.

Sent from my BlackBerry smartphone from Sinyal Bagus XL, Nyambung Teruuusss...!
Google Groups "Indonesian Android Community [id-android]" group.

To post to this group, send email to id-android@googlegroups.com

To request to subscribe to this group:


2. android.process.acore is giving me lots of trouble. please help!!

What phone/model do you have? I suggest getting it fixed from where you
bought it?


I am here cause my phone has pretty much become useless,
I did a hard reset on my phone ( more like a 100 of them ) & still no
luck...my touch screen is not working, I cant make or receive any
calls, i can't access my contacts, nor can I send new texts unless
people text me first.
If i try to access any of it it tells me that android.process.acore
has to force close.

I do not believe its any app as I've done a hard reset on my phone & I
am still having the same problem.

It is so frustrating cause I baby the phone & this would happen to me.
I'd appreciate it if anyone could help.


3. Multipple email accounts in Gmail

4. Regarding the BT pairing when android is in suspend

5. The Archos AppLib store.

6. Unlock Motorola Cliq and use with AT&T?

7. This class was probably produced by a broken compiler.