0

我目前在 Mac OS X 应用程序中使用 Apple 的 SimplePing 在传输数据之前对 URL 执行 ping 操作,效果很好,但会锁定我的 UI。我可能没有找对地方,但我该如何防止这种情况发生?我目前正在使用 currentRunLoop,我认为这是问题所在,但我仍然希望用户能够在此操作期间与 UI 进行交互(例如取消)。如何为 Simple Ping 创建一个运行循环,以便我的 UI 不会锁定?

SimplePing *localPing = [SimplePing simplePingWithHostName:pingHost];
        [localPing setDelegate:self];
        [localPing start];

        do {
            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
        } while (localPing != nil);
4

1 回答 1

0

使用 SimplePing 时您的 UI 被锁定的原因是 ping 实用程序需要一定时间才能完成。而且您似乎是在主线程中执行此操作,导致锁定 UI 界面白 ping 任务正在进行中。所以你可以使用下面的代码

-(void) ping:(NSString *) ip
{
SimplePing *localPing = [SimplePing simplePingWithHostName:pingHost];
[localPing setDelegate:self];
[localPing start];
}

然后在新线程中调用 ping 函数

[NSThread detachNewThreadSelector:@selector(ping:) toTarget:self. withObject:@"IP Address"];
于 2012-08-08T10:32:53.050 回答