开发者

to stop a running method

开发者 https://www.devze.com 2023-01-17 15:43 出处:网络
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 myMe

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消