开发者

Cancel AsyncTask

开发者 https://www.devze.com 2023-03-31 02:15 出处:网络
Is it possible to cancel AsyncTask that has one line command in doInBackground that performs a 开发者_StackOverflow中文版very long operation,like

Is it possible to cancel AsyncTask that has one line command in doInBackground that performs a 开发者_StackOverflow中文版very long operation,like

@Override
protected Boolean doInBackground(String... filename) {
    fetchfile(filename[0]);
    // ...
    // ...
    return ...;
}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号