Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

So I have a method that performs a loop and because of the way my program works i'm calling this method using,

[self performSelectorInBackground:@selector(myMethod:) withObject:arg];

And the method itself,

- (void)myMethod:(id)arg {
    //...

    for (//Loop Conditions) {
        if (stopMorse) {
            break;
        }
        //Do stuff
    }

    //...
}

Now I would like to allow the user to stop this loop from running, so is there a way of killing/stopping the thread.

I've tried adding a BOOL but the problem is that it doesn't seem to get updated because it's on a separate thread. What is the best way to stop this loop from running?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
380 views
Welcome To Ask or Share your Answers For Others

1 Answer

You shouldn't forcibly kill/stop the thread for reasons outlined in this answer to a similar question, and this answer to another similar question.

It should be possible to check a boolean as you are doing, and there shouldn't be a problem with another thread setting the value of the boolean. Can you post the code that is attempting to set the boolean?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...