Re: onPause() Why u no do the things I want?

by TreKing » Wed, 23 Mar 2011 07:10:14 GMT


Sponsored Links
 



 http://developer.android.com/reference/android/os/AsyncTask.html 

< http://developer.android.com/reference/android/os/AsyncTask.html> ;Read
"Canceling a Task".

-------------------------------------------------------------------------------------------------
TreKing < http://sites.google.com/site/rezmobileapps/treking> ; - Chicago
transit tracking app for Android-powered devices

-- 



Re: onPause() Why u no do the things I want?

by Tobiah » Wed, 23 Mar 2011 23:17:09 GMT


 
Logging shows me that the onPause() method is being called, yet the AsyncTask is still running while I'm viewing the other activity. http://developer.android.com/reference/android/os/AsyncTask.html < http://developer.android.com/reference/android/os/AsyncTask.html> ;Read "Canceling a Task".
Ok, I see that I have to check isCancelled() from within doInBackGround() and exit myself. What confused me was the boolean argument to cancel(). mayInterruptIfRunning true if the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete. So I thought that by calling cancel(true), the task would be whacked. What does that argument really do then? Thanks, Tobiah --


Sponsored Links


Re: onPause() Why u no do the things I want?

by Kostya Vasilyev » Wed, 23 Mar 2011 23:26:08 GMT


 23.03.2011 18:16, Tobiah :
So I thought that by calling cancel(true), the task would be whacked. What does that argument really do then?
According to the docs, calling cancel(true) causes the task's worker thread to be interrupted. See Thread.interrupt: http://developer.android.com/reference/java/lang/Thread.html #interrupt() Which, simplifying things somewhat, has a similar effect to just cancel(false), in that the worker code still has to check for interruption (just like async task has to check for cancellation). In short - task.cancel(true) is not magic :) -- Kostya Vasilyev -- http://kmansoft.wordpress.com --



Re: onPause() Why u no do the things I want?

by TreKing » Wed, 23 Mar 2011 23:33:09 GMT


 




AsyncTask manages a thread to do it's work. According to the doc comment, it
probably calls 
interrupt()< http://developer.android.com/reference/java/lang/Thread.html #interrupt()>on
the thread, which, according to the docs, doesn't really do much
unless
the thread is in an "interruptible state". If your task is chugging along,
the thread won't be interrupted. So cancel(true) is fairly pointless unless
your thread is sitting around waiting for something.

-------------------------------------------------------------------------------------------------
TreKing < http://sites.google.com/site/rezmobileapps/treking> ; - Chicago
transit tracking app for Android-powered devices

-- 



Re: onPause() Why u no do the things I want?

by Tobiah » Fri, 25 Mar 2011 23:09:07 GMT


 

On Wed, Mar 23, 2011 at 10:16 AM, Tobiah <t...@tobiah.org What does that argument really do then? AsyncTask manages a thread to do it's work. According to the doc comment, it probably calls interrupt() < http://developer.android.com/reference/java/lang/Thread.html #interrupt()> on the thread, which, according to the docs, doesn't really do much unless the thread is in an "interruptible state". If your task is chugging along, the thread won't be interrupted. So cancel(true) is fairly pointless unless your thread is sitting around waiting for something.
The task looks like this: while(true): quickly_do_something() sleep(1000) So, it is "waiting" in a sense because of the sleep, but maybe you mean something else by "waiting"? Thanks, Tobiah --



Re: onPause() Why u no do the things I want?

by Kostya Vasilyev » Fri, 25 Mar 2011 23:29:39 GMT


 Ah.

In this case, calling cancel with "true" does make sense, as it will 
make sleep() abort immediately with an InteruptedException.


However, the loop still need to check AsyncTask.isCancelled().

More info on interrupting threads can be found here:

 http://developer.android.com/reference/java/lang/Thread.html #interrupt()

-- Kostya

25.03.2011 18:08, Tobiah :
The task looks like this: while(true): quickly_do_something() sleep(1000) So, it is "waiting" in a sense because of the sleep, but maybe you mean something else by "waiting"? Thanks, Tobiah
-- Kostya Vasilyev -- http://kmansoft.wordpress.com --



Re: onPause() Why u no do the things I want?

by TreKing » Fri, 25 Mar 2011 23:38:10 GMT


 



I don't, that's the idea. But you'd only get interrupted while in the
sleep(). You'd want something like

while (!isCanceled()):
{
doSomething();

if (!isCanceled())
 sleep(1000);
}

Although, that seems like a strange use of AsyncTask.

-------------------------------------------------------------------------------------------------
TreKing < http://sites.google.com/site/rezmobileapps/treking> ; - Chicago
transit tracking app for Android-powered devices

-- 



Re: onPause() Why u no do the things I want?

by Tobiah » Fri, 25 Mar 2011 23:39:51 GMT


 

Ah. In this case, calling cancel with "true" does make sense, as it will make sleep() abort immediately with an InteruptedException. However, the loop still need to check AsyncTask.isCancelled().
So am I to understand then, that the only thing that will change by calling cancel(true) is that I would not have to wait for the entire remaining duration of the sleep for the task to end, whereas with cancel(false), the sleep would complete before I could check isCancelled() and exit? Thanks, Tobiah
More info on interrupting threads can be found here: http://developer.android.com/reference/java/lang/Thread.html #interrupt() -- Kostya 25.03.2011 18:08, Tobiah :
The task looks like this: while(true): quickly_do_something() sleep(1000) So, it is "waiting" in a sense because of the sleep, but maybe you mean something else by "waiting"? Thanks, Tobiah
--



Re: onPause() Why u no do the things I want?

by Kostya Vasilyev » Fri, 25 Mar 2011 23:42:23 GMT


 Exactly. Which to me seems like a very valuable thing.

25.03.2011 18:39, Tobiah :
So am I to understand then, that the only thing that will change by calling cancel(true) is that I would not have to wait for the entire remaining duration of the sleep for the task to end, whereas with cancel(false), the sleep would complete before I could check isCancelled() and exit? Thanks, Tobiah
-- Kostya Vasilyev -- http://kmansoft.wordpress.com --



Re: onPause() Why u no do the things I want?

by TreKing » Fri, 25 Mar 2011 23:42:56 GMT


 



I believe so, yes. You can easily test this by putting in a massive sleep
period and observing what happens when you cancel() with either true or
false.

-------------------------------------------------------------------------------------------------
TreKing < http://sites.google.com/site/rezmobileapps/treking> ; - Chicago
transit tracking app for Android-powered devices

-- 



Re: onPause() Why u no do the things I want?

by Tobiah » Fri, 25 Mar 2011 23:48:40 GMT


 
So, it is "waiting" in a sense because of the sleep, but maybe you mean something else by "waiting"? I don't, that's the idea. But you'd only get interrupted while in the sleep(). You'd want something like while (!isCanceled()): { doSomething(); if (!isCanceled()) sleep(1000); }
That's basically what I'm doing. I take it that the boolean argument to cancel() just gets me a quicker exit rather than waiting for the sleep to continue?
Although, that seems like a strange use of AsyncTask.
I have a ListView that displays a list of people. While that Activity is visible, it is possible for another thread to query a server and get another person. The AsyncTask in question queries the database once a second to see whether there are any new records. If there are, the ListView updates accordingly. Is that strange? Also, what of my choice to check the database once a second? To often? Can I go to 1/10 of a second without impacting CPU or battery usage? Thanks, Toby --



Re: onPause() Why u no do the things I want?

by Kostya Vasilyev » Sat, 26 Mar 2011 00:00:54 GMT


 25.03.2011 18:47, Tobiah :
Also, what of my choice to check the database once a second? To often? Can I go to 1/10 of a second without impacting CPU or battery usage?
Rather than polling like that, I would post a message of some sort, which would probably end up calling notifyDataSetChanged. If you are using a content provider for your data query, you can trigger an update with: http://developer.android.com/reference/android/content/ContentResolver.html #notifyChange(android.net.Uri, android.database.ContentObserver) -- Kostya Vasilyev -- http://kmansoft.wordpress.com --



Re: onPause() Why u no do the things I want?

by TreKing » Sat, 26 Mar 2011 00:09:54 GMT


 



Not really. Your code showed an infinite loop with no way of breaking out.
You need *some* way for your task to complete.



Basically, yes. Also note that sleep() is not the only interruptible state.
I think wait() and join() are as well, so it depends how complicated you're
getting with task's thread.



I think so. In my mind an AsyncTask represents a single, executable action
that does one thing that may take a while and then exits. I wouldn't use it
to do a looping action like this.

If this other thread queries your servers and finds a new person, I would
post a message on a Handler to indicate to your Activity to add the new
person. No need to hit the DB every 1 second.



Terrible :-) See above comment.



Yes, too often, IMO. Any extra work you do uses the CPU which uses the
battery.

-------------------------------------------------------------------------------------------------
TreKing < http://sites.google.com/site/rezmobileapps/treking> ; - Chicago
transit tracking app for Android-powered devices

-- 



Re: onPause() Why u no do the things I want?

by Nathan » Sat, 26 Mar 2011 01:03:29 GMT


 I believe that sleep, and other blocking operations will throw an
interruptedexception when interrupted. Keep that in mind.

I've tried the interrupt before while I was doing long blocking IO
operations. It didn't work, I realized, because I was handling the
exception as an IOException and retrying instead of exiting.

Nathan

-- 



Re: onPause() Why u no do the things I want?

by Streets Of Boston » Sat, 26 Mar 2011 11:15:50 GMT


 Very true, Nathan.

When calling cancel(true), an interrupt is 'called' on the thread. This 
means that methods that put the thread into a wait-state (wait, sleep and 
such) get interrupted and these will throw an interrupted-exception. Some 
(not all!) blocking IO operation may get interrupted as well, but they are 
not guaranteed to throw an interrupted-exception, as you found out in your 
case. Instead, a io-exception was thrown.

In short, 
when calling cancel(false), you'd only have to deal with the isCancelled 
method. But blocking operations won't get interrupted.
when calling cancel(true), you'd have to deal with the isCancelled method 
and blocking operation will get interrupted somehow. The downside to these 
interrupted operations is that you need to deal with them properly (catching 
the appropriate exceptions and dealing with them).

--