Re: Best way to handle this
Re: Best way to handle this
- Subject: Re: Best way to handle this
- From: Nathan Kinsinger <email@hidden>
- Date: Thu, 8 Jan 2009 18:49:24 -0700
On Jan 8, 2009, at 4:20 PM, development2 wrote:
I am hoping someone can help me figure out the best way to handle
this, I am sure someone out there has some experience with this.
I have some code that sends a command to an external hardware device
and waits to get the response back with the info that was requested
of the device. Now what I want to do is animate on screen the
graphic of the device, showing that it is working. What I tried to
do at first was setup an NSTimer that would be called every 1 second
say, then call my method to call the device and wait for the device
to respond. But basically my call blocks the NSTimer because it
takes some time (actually what I am calling from my code is a
command line tool that we created that will return a result file to
us). So what I want to do is call my method to get the device info
and while waiting for the response animate this graphic of the
device in operation. What is the best way to do this?
1) Should I use an NSThread for this?
2) IS there some other Cocoa functionality that might help me here?
Thanks in advance for any help.
The short answer, yes you need to use threads.
However NSThread is not the only way, the performSelector... set of
methods can be useful in situations like this.
<http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/doc/uid/20000050-SW20
>
In particular, - performSelectorInBackground:withObject: for the
method that runs the command line tool. Then the main thread is free
to update the graphics in your NSTimer. The UI graphics need to be
updated from the main thread, so don't block it if at all possible.
When the command line tool returns, call -
performSelectorOnMainThread:withObject:waitUntilDone: or set a flag,
so you know when the response has been received.
--Nathan
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden