Objective c 简单NSThread或NSTimer
我想运行某些后台任务 场景:我想要一个按钮来激活线程或计时器,然后让线程/计时器开始每秒重复一次,将nsrunformationalertpanel返回给用户,并返回数据 这是我的计时器:Objective c 简单NSThread或NSTimer,objective-c,xcode,nstimer,nsthread,Objective C,Xcode,Nstimer,Nsthread,我想运行某些后台任务 场景:我想要一个按钮来激活线程或计时器,然后让线程/计时器开始每秒重复一次,将nsrunformationalertpanel返回给用户,并返回数据 这是我的计时器: -(void)workerThread:(NSTimer*) theTimer { if(intNumberOfTicks > 0) { NSRunInformationalAlertPanel(@"The Serial", [NSString stringWithFo
-(void)workerThread:(NSTimer*) theTimer {
if(intNumberOfTicks > 0)
{
NSRunInformationalAlertPanel(@"The Serial", [NSString stringWithFormat:@"%d", intNumberOfTicks], @"OK", nil, nil);
//[txtTimeMinutes setStringValue:[NSString stringWithFormat:@"%d", intNumberOfTicks]];
intNumberOfTicks--;
}
else {
[timer invalidate];
}
}
为了启动这个方法
intNumberOfTicks = 5;
timer = [[NSTimer scheduledTimerWithTimeInterval:1 target: self selector:@selector(workerThread:) userInfo:self repeats:true] retain];
// Or for threading...
///[NSThread detachNewThreadSelector:@selector(workerThread) toTarget:self withObject:nil];
有人能帮我实现我所需要的吗?也许可以提供NSThread或NSTimer的最基本示例。我已经查看了Apple开发参考资料,但运气不好。使用NSTimer将在实例化和调用选择器的线程中执行选择器 如果您的任务必须在后台线程中执行,请尝试调用performSelectorInBackground:withObject: :
在后台线程中,您可以按照上述方式使用定时计时器。什么不起作用?这看起来是正确的,只是将
self
作为计时器的userInfo
参数传递没有任何意义。哦,您不需要保留计时器。当代码运行nothing时,nsrunformationalertpanel将不显示,get将输出到屏幕