2
votes

Please don't think it's a redundant question. Please read completely.

I have an infinite loop inside the doInBackground(Void... params) of AsyncTask. And there's a callback function within doInBackground(). So, I am not able to terminate this task using cancel(), onCancelled() and isCancelled() approach. The problem is the execution waits at the callback function forever. So, no matter where (within doInBackground) I write this isCancelled() check to break out of the infinite loop, it doesnt get executed.

Does anyone have a solution for this kind of problem? Or anyone had a requirement similar to this?

@Override
protected void onCancelled()
{
    // Socket Close
}

@Override
    protected Void doInBackground(Void... params)
    {
        try
        {
            // Socket Open

            while (true)
            {

                socket.receive();
            }
        }
        catch (Exception e)
        {

        }
        return null;
    }
3
What do you mean by "callback function"? A callback is an interface, not a function. Is that socket.receive? I guess it is as it's the only thing in there. What does your socket.receive look like? Sorry, but I've read your question several times and can't understand exactly what you are asking.Simon
I don't understand what the problem is.wtsang02
Hi Simon, socket.receive() is nothing but DatagramSocket's receive method (docs.oracle.com/javase/1.4.2/docs/api/java/net/…. This thread actually listens to any incoming UDP packets.Karthik Andhamil

3 Answers

1
votes

Before you start with any receiving operations, call socket.setSoTimeout(int timeout) and give it a timeout according to your needs (If you usually wait a little time for data, you can use 1000ms or less. If it might take long to receive data, use a greater value, 10000ms for example).

Then, in your loop, after socket.receive method, check if the task was cancelled (isCancelled() == true) and if it was, break out from the loop.

1
votes

You should use a timeout in your socket.receive(), so from time to time it passes control back to AsyncTask, where you may check isCancelled() and terminate the loop.

1
votes
        @Override
    protected void onCancelled()
    {
        // Socket Close
    }

    @Override
        protected Void doInBackground(Void... params)
        {
            try
            {
                // Socket Open

                while (!isCancelled())
                {

                    socket.receive();
                    OnProgressUpdate(...);
                }
            }
            catch (Exception e)
            {

            }
            return null;
        }
    @Override
        protected void onProgressUpdate(...) {
                receiveing=true;
            }

boolean receiveing;

Since I don't know what you are trying to achieve, and the question wasn't clear, I am not sure if this is what you are asking. You would put the cancel() where you need it.
And infinite loop is not a good idea...