0

I'm currently using Apple's SimplePing in a Mac OS X application to ping a URL before transferring data, which works fine, but locks up my UI. I may not have looked in the right places, but how do I keep this from happening? I'm currently using the currentRunLoop, which I think is the problem, but I still want the user to be able to interact with the UI (e.g. cancel) during this action. How do I create a run loop for Simple Ping so my UI doesn't lock up?

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

        do {
            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
        } while (localPing != nil);
Katie
  • 253
  • 1
  • 3
  • 13

1 Answers1

0

This reason your UI is locked while using SimplePing is because ping utility take certain time to complete. And it seems that you are doing this in your main thread, resulting locking the UI interface white ping task is in process. So you can use following code

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

and then call ping function in new thread like

[NSThread detachNewThreadSelector:@selector(ping:) toTarget:self. withObject:@"IP Address"];
Black Tiger
  • 1,201
  • 11
  • 12
  • 1
    This seems to solve the UI lockup problem, but I never seem to be getting the delegate callback, so my application never completes. Any thoughts on why this might be/how to fix it? The only things I changed in your answer was to add an autoreleasepool and change `ip` to `pingHost` in `ping:` and take out the extraneous period after self in `detachNewThreadSelector:` – Katie Aug 08 '12 at 15:33