to stop a running method
i want to stop cu开发者_如何转开发rrently running method for a short duration.. can i do it without using any thread ?
Split the method into a myMethodPartA
and a myMethodPartB
. Then, at the end of myMethodPartA
, use the line:
[self performSelector: @selector(myMethodPartB) withObject: yourArgument afterDelay: someNSTimeInterval]
If you need to pass a bunch of information from myMethodPartA
to myMethodPartB
, you can bundle all of that information into an NSArray which you pass as the argument to myMethodPartB
.
Use NSTimer:
- (void)doThing
{
[self doFirstPart];
[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(doSecondPart)
userInfo:nil
repeats:NO];
}
- (void)doFirstPart { printf("Hello, "); }
- (void)doSecondPart { printf(" world!\n"); }
You can always use the sleep()
or usleep()
functions. There might be convenient methods available on NSThread
as well, depending what you use exactly (you often work on one thread anyway).
精彩评论