Android - Cancel Asynctask Forcefully

Android - Cancel AsyncTask Forcefully

Just check isCancelled() once in a while:

 protected Object doInBackground(Object... x) {
while (/* condition */) {
// work...
if (isCancelled()) break;
}
return null;
}

How to cancel or stop AsyncTask?

On your button click write the code

task.cancel(true);

where task is the reference of the async task you have created and started

Android how to stop AsyncTask

The cancel(...) method prevents the call of onPostExecute() after doInBackground() finishes. Instead onCancelled() will be invoked. Furthermore isCancelled() will return true as soon as you call cancel(). You should check for this in your doInBackground(), to be able to let it finish ASAP.

So, this is useless code:

@Override
protected void onCancelled() {
super.onCancelled();
cancel(true);
}

cancel(true) has no effect here, because onCancelled() will only be invoked if you call cancel() before. You can leave it empty or deal with the situation here if your AsyncTask will be canceled.

Here you should check for isCancelled():

 @Override
protected Boolean doInBackground(Void... params) {
return null;
}

Because you do nothing here your AsyncTask is useless, because you do no background threading at all.

This is the wrong place to call isCancelled():

@Override
protected void onPreExecute() {

if (!isCancelled()){
...
}

Again, isCancelled() should be checked in doInBackground() to let it finish ASAP.

To cancel your Task by switching Activities or an Activity goes to background, use the onPause() callback and call mTask.cancel(true);
Read the docs about the AsyncTask carefully again to get the basics.

Ideal way to cancel an executing AsyncTask

Just discovered that AlertDialogs's boolean cancel(...); I've been using everywhere actually does nothing. Great.

So...

public class MyTask extends AsyncTask<Void, Void, Void> {

private volatile boolean running = true;
private final ProgressDialog progressDialog;

public MyTask(Context ctx) {
progressDialog = gimmeOne(ctx);

progressDialog.setCancelable(true);
progressDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// actually could set running = false; right here, but I'll
// stick to contract.
cancel(true);
}
});

}

@Override
protected void onPreExecute() {
progressDialog.show();
}

@Override
protected void onCancelled() {
running = false;
}

@Override
protected Void doInBackground(Void... params) {

while (running) {
// does the hard work
}
return null;
}

// ...

}

To force cancel AsyncTask shouldn't the flag periodically checked in doInBackground be volatile?

Yes, it should be volatile. Otherwise a write to the variable in thread A may not be visible to a read in thread B due to optimizaton(by compiler, JVM, etc). See this

Cancel all AsyncTask?

just iterate through all list & use the following which will cancel all running asynctask.

if(myAsyncTask.getStatus().equals(AsyncTask.Status.RUNNING))
{
myAsyncTask.cancel(true);
}

Force stop a blocking read AsyncTask

java.io.InputStream implements blocking I/O. It is expected that your application will block on

myInputstream.read(readbyte)

until the read operation will return -1 which means that there no more data to read from stream (as it was closed or connection was terminated) or throw the exception.

The only way to unblock the thread waiting for data on stream is to close it. In your case you need to be able to close the Socket created in

mySocket = new Socket();

line.

mySocket.close();

should interrupt your reading. But stay aware that Socket.close() actually

Closes this socket.

Any thread currently blocked in an I/O operation upon this socket will throw a SocketException.

So you task tread will get unblocked but you have to handle the SocketException correctly in your task to terminate all operations (maybe this is what you are missing).

FYI calling the

task.cancel(true);

doesn't make much sense as this only sets the cancelled flag on task. It is responsibility of your code to check for the flag and with blocking I/O you have no possibility to do it.

Alternatively you should consider using non-blocking I/O.



Related Topics



Leave a reply



Submit