0

Is it possible to cancel AsyncTask that has one line command in doInBackground that performs a very long operation,like

@Override
protected Boolean doInBackground(String... filename) {
    fetchfile(filename[0]);
    // ...
    // ...
    return ...;
}
joschi
  • 12,746
  • 4
  • 44
  • 50
azzits
  • 345
  • 2
  • 3
  • 10

2 Answers2

0

You can AsyncTask.cancel() to cancel. You can cancel it inside the fetchFile method at an appropriate place where you feel it will not introduce inconsistencies in your data or at a place where its easier to rollback.

Ron
  • 24,175
  • 8
  • 56
  • 97
0

In your long run method, periodically check isCanceled, if it's true, return from function ASAP so that async thread may be closed. And you may call cancel() from any thread.

Pointer Null
  • 39,597
  • 13
  • 90
  • 111