Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [NSApp runModalSession] from Carbon app




Le 6 avr. 08 à 08:55, Kurt Sutter a écrit :
I have a carbon app that should bring up a modal cocoa window. The app is multithreaded, with some of the threads being cooperative.

To bring up the modal window, I originally called [NSApp runModalForWindow], but I noted that this would not allow my cooperative thread to run. I therefore tried [NSApp runModalSession], e.g. as follows:

CocoaDialog* controller = [[ConvolutionDialog alloc] initWithNib:@"Convolution"];
NSModalSession session = [NSApp beginModalSessionForWindow:[controller window]];

while (1)
{ if ([NSApp runModalSession:session] != NSRunContinuesResponse)
break;
YieldToAnyThread();
}
[NSApp endModalSession:session];

[controller release];

I was hoping that runModalSession would return to the loop whenever it had nothing to do (according to the documentation, as far as I understood it, it should do that) so I can call YieldToAnyThread, giving my cooperative threads an opportunity to execute. However, I note that runModalSession returns only when the window is dismissed, and therefore I don't get the opportunity to call YieldToAnyThread.

Any advice what I am doing wrong here? Does runModalSession work as designed, or is this a bug? Any other way for achieving what I am trying to do?

TIA

Kurt Sutter

You should not use runModalSession this way.
See the discussion part in this method documentation:

“You must invoke this method frequently enough in your loop that the window remains responsive to events. However, you should not invoke this method in a tight loop because it returns immediately if there are no events, and consequently you could end up polling for events rather than blocking.

Typically, you use this method in situations where you want to do some additional background processing while the modal loop runs. For example, while processing a large data set, you might want to use a modal dialog to display progress and give the user a chance to cancel the operation. If you want to display a modal dialog and do not need to do any additional work in parallel, use runModalForWindow: instead. When there are no pending events, that method waits idly instead of consuming CPU time.”


This is maybe not clear, but you should use this method only if you plan to do some background processing on the main thread.

If you do some background processing in other thread, do not bother with manual session management and use  - [NSApplication runModalForWindow:] instead.

Basically, your code should be:

CocoaDialog *controller = [[ConvolutionDialog alloc] initWithNib:@"Convolution"];
[NSApp runModalForWindow:[controller window]];
[controller release];


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Carbon-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/carbon-dev/email@hidden

This email sent to email@hidden

References: 
 >[NSApp runModalSession] from Carbon app (From: Kurt Sutter <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.