How to stop a for via external loop interference?

Asked

Viewed 67 times

0

I have a for running and clicking on the button I want this for stop.

           for (int i = 0; i < codigo.size(); i++) {


            if (codigo.get(i).contains("Para") || Stop == true){

                sendCommand("parou");

                break;

            } else {
                sendCommand(codigo.get(i));

            }

I’m sending you value like String when he finds the word "to" he comes out of the for, put a guy boolean Stop when true it would come out of the loop too, I change the value by pressing a button but it didn’t work. I wish you could get out for saw a button or if there would be another way to do it.

  • You want, for example, if a condition is false out of the loop?

  • 1

    It would not be the case to use the command break?

  • In the title of your question says one thing and in the body says another. I suggest you make it clearer.

  • 2

    There is no stopping the execution of a cycle for at the click of a button. This is because the interruption would have to be done inside the for. And the onClick of the button usually stays out of the loop... Maybe it would be better if you told what you want to do. What is the cycle for?

  • Let me guess, the thread running this for is the same one that should notice the click of the button. I got it right? Because if that is so, you would have to have the same thread running two lines simultaneously, which you can’t do since a thread is by definition a line of execution. The solution would be to run this yours for within a separate thread.

  • 1

    @Victorstafusa had thought about it but hadn’t tested it, tested it as a new thread and it worked, thanks.

Show 1 more comment

1 answer

1

   boolean keepGoing = true;

    for (int i = 0; i < codigo.size(); i++) {
        if (keepGoing) {
            i++;

        } else {
            return;
        }
    }

    void onClick(view v){
        keepGoing = false;
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.