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
AsyncTask.

--



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
 http://www.developer.com/java/j2me/article.php/10934_3762056_3/Handling-Lengthy-Operations-in-Googles-Android.htm 

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
example, MESSAGE_LONG_RUNNING_TASK_RESULT.
2. As part of my Activity class, define a message handler which
responds to MESSAGE_LONG_RUNNING_TASK_RESULT.
3. Start a new thread. For the purpose of this discussion, call it
"ControllerThread".
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. why glReadPixels return Alpha always -1

Hi,

it is always return -1 when call Android opengl es api glReadPixels(),
it is a bug of Android?

Thanks,

--~--~---------~--~----~------------~-------~--~----~

2. ListView onListItemClick to Create a Context Menu?

If you use a ListActivity, try calling getListView().showContextMenu()
in your onListItemClick() implementation.

Nivek



--~--~---------~--~----~------------~-------~--~----~

3. Click on Status bar notification switch to task in current state?

4. Close Application

5. Click on Status bar notification switch to task in current state?

6. space between component

7. URL