开发者

Run code for a certain length of time and kill if necessary

开发者 https://www.devze.com 2022-12-21 04:21 出处:网络
I\'m using the scripting bridge to query iTunes from my cocoa application. Sometimes iTunes pops up a window (like if an ipod needs updating etc.) and while that popup window is open I can\'t get any

I'm using the scripting bridge to query iTunes from my cocoa application. Sometimes iTunes pops up a window (like if an ipod needs updating etc.) and while that popup window is open I can't get any information from iTunes. So if I request information from iTunes when it's in this state my application completely locks-up until that popup window is dismissed.

So I need some sort of mechanism where I can ask itunes something simple in a separate thread to see if I can get a response from it... and if that separate thread doesn't receive a response within a short period of time my 开发者_StackOverflow中文版main thread will just kill that thread and thus know not to query itunes at that particular time.

Any ideas how to create such a mechanism? I searched for ways to kill a thread but haven't found any.


Your problem is nothing to do with threads; it's that your timeout is too long. Whatever you're doing should fail after about a minute.

To fix this, send a setTimeout: message to the SBApplication object, passing the amount of time you want it to wait. The value is in ticks, of which there are exactly 60 per second.

(Some sources say 60.15, and Apple's own docs say “approximately” 60, but I just measured ten minutes' worth of TickCount, and the result of the division by 600 seconds is exactly 60.0. The code I used:
NSLog(@"Ticks per second: %f", (end - start) / (60.0 * numMinutes)); where end and start are results from TickCount.)


Check out NSOperation/NSOperationQueue.

0

精彩评论

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

关注公众号