Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > How to relinquish control to the GUI?

How to relinquish control to the GUI?
Thread Tools
Zimwy
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
Feb 25, 2006, 01:48 AM
 
Hi,
Let's say I have the following:
Code:
NSProgressIndicator* progress; (assume it's initialized and visible etc) for (int i = 0; i < 5; ++i) { [progress setDoubleValue: (double) i]; }
I tried the following:
Code:
NSProgressIndicator* progress; (assume it's initialized and visible etc) for (int i = 0; i < 5; ++i) { [progress setDoubleValue: (double) i]; sched_yield(); }
but that didn't work. I know that having an NSTimer constantly call back to update the progress indicator is a good thing to do, but the circumstances make that very ugly. Any ideas?

thanks,
gabe
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Feb 25, 2006, 03:27 AM
 
I think NSRunLoop is what you're looking for.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
smeger
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Feb 25, 2006, 08:24 AM
 
When you update the value of a progress indicator, it's marked as needing display the next time through the runloop (with setNeedsDisplay: YES). Your code, however, never returns to the runloop, so it's not redrawn. You can manually redraw it each time through by calling [progress display].

A better bet is to stick the loop into a separate thread that messages the main loop with the progress bar value. The main loop then calls setDoubleValue, and the progress bar can update whenever the processor has enough cycles. Look into Distributed Objects, or use pthreads.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
Catfish_Man
Mac Elite
Join Date: Aug 2001
Status: Offline
Reply With Quote
Feb 25, 2006, 01:56 PM
 
Originally Posted by smeger
When you update the value of a progress indicator, it's marked as needing display the next time through the runloop (with setNeedsDisplay: YES). Your code, however, never returns to the runloop, so it's not redrawn. You can manually redraw it each time through by calling [progress display].

A better bet is to stick the loop into a separate thread that messages the main loop with the progress bar value. The main loop then calls setDoubleValue, and the progress bar can update whenever the processor has enough cycles. Look into Distributed Objects, or use pthreads.
Or he could just use an NSTimer and save himself a lot of effort...
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Feb 25, 2006, 02:34 PM
 
He already said that, for some reason, an NSTimer is really impractical for what he's doing. I'd think that indicates a problem with the program's structure, but y'know, whatever. His program.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Zimwy  (op)
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
Feb 25, 2006, 02:56 PM
 
Thanks for the responses. I was hoping to avoid pthreads as it's a bit overkill. As for some people who are unhappy with my design I can elaborate as to why it's uncomfortable to use an NSTimer (which is what I ended up doing anyway). Here is more or less the code before the NSTimer:

Code:
int totalCorrect[numTypes]; int tmpData[numTypes_]; memset(totalCorrect, 0, numTypes * sizeof(int)); int maxCorrect = 0; for (int n = 0; n < trainingTypes_; ++n) { memset(tmpData, 0, numTypes * sizeof(int)); [self computeNumCorrectInto: tmpData limitedTo: n]; if (arraySum(tmpData) > maxCorrect) maxCorrect = arraySum(tmpData); }
where arraySum just sums up the ints of the array and computeNumCorrectInto: fills in the array passed to it.

Now instead, since I have things split over different functions with NSTimers I've had to make a bunch of things member variables such as the n that was passed to computeNumCorrectInto and the totalCorrect array. Just seems messier.

gabe
     
Wevah
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
Feb 26, 2006, 03:07 PM
 
Couldn't you just pass those things in as the timer's userInfo object (e.g., in an NSDictionary)?
[Wevah setPostCount:[Wevah postCount] + 1];
     
Zimwy  (op)
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
Feb 28, 2006, 10:37 AM
 
I don't think that would work. If I declared the array on the stack and then passed it into the user info of the timer wouldn't it already be popped off the stack by the time the timer goes off? Maybe not the first time, but it's a timer that repeats for about 5 mins, so it would definitely be gone soon. No?
I feel like allocating memory on the heap for this is just ridiculous but that's how it has to be.

gabe
     
Wevah
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
Feb 28, 2006, 10:56 AM
 
Yeah, it would be off the stack; that's why I suggested an NSDictionary. Actually, the userInfo object is declared to be an id, so it should be an Objective-C object, anyway (it's retained by the timer).
[Wevah setPostCount:[Wevah postCount] + 1];
     
Zimwy  (op)
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
Mar 1, 2006, 10:39 AM
 
Ahh, I didn't know the userinfo got retained by the timer. That's very interesting. Thanks for the info!

gabe
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 11:13 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,