私は財団ツールを書いています。進行中のさまざまなタスクを分離するには、スレッド化を行う必要があります。
スレッド化を試みましたが、継続的にクラッシュしていました。そして最後に、独自の実行ループを実行する必要がある理由を突き止めました。
簡単な例を手伝ってくれる人はいますか?次のコードを試しましたが、うまくいきません。実行するたびに例外でクラッシュしますか? Foundation ツールでスレッドを実行するにはどうすればよいですか?
@interface MyClass : NSObject
{
}
-(void) doSomething;
@end
@implementation MyClass
-(void) RunProcess:
{
printf("test");
}
-(void) doSomething:
{
[NSThread detachNewThreadSelector: @selector(RunProcess:) toTarget:self withObject: nil];
}
@end
int main(void)
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
MyClass *myObj = [[MyClass alloc] init],
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 myObj selector:@selector(doSomething:) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
[pool drain];
return 0;
}