[android-developers] AsyncTask vs Thread?
I have an operation that consumes too much time for the UI thread and
need to do it in another thread. I created a thread using AsyncTask
and the thread loops until it finds and answer. When its done, I use
postExecute to deal with the data and update views. The problem is
that the thread intermittently stops in the middle of a long loop with
no errors. Sometimes it goes through all data looking, sometimes, the
thread returns having only looped through a portion.
I posted a question on Stackoverflow, but the only answer I got was
not to use AsyncTask but just use threads without providing any
reasoning or details why one is better than another.
What do folks here think? Is there something wrong with the way I've
implemented my AsyncTask? Or is the stackoverflow poster correct in
that I should just blindly use Thread without understanding why this
is incorrect, good, bad, ugly or whatever.
private void playDeviceTile() {
if (mDbHelper.isPlayingDevice(game_id)) {
// make sure I'm seeing all played tiles
refreshPlayedTiles();
final Boolean found;
final ProgressDialog dialog = new ProgressDialog(mcontext);
dialog.setMessage("Android is thinking...");
dialog.show();
new AsyncTask<Void,Void, Boolean>(){
@Override
protected void onPostExecute(Boolean isFound) {
if (!isFound) {
passPlay(1); // never found a tile to
play. We have to pass
}
else {
playTile(currentTile,1);
}
dialog.dismiss();
postInvalidate();
invalidate();
}
@Override
protected Boolean doInBackground(Void... params) {
try {
return doSearchforSpot();
} catch (Exception e) {
Log.e("DRAW", "Exception find spot for device
tile", e);
}
return null;
}
}.execute();
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home