Skip navigation.
 
mlRe: Using NSThreads in command-line apps
FROM : Jens Alfke
DATE : Thu May 15 20:52:00 2008

On 15 May '08, at 11:42 AM, ben syverson wrote:

> Actually, I'm able to spawn new threads perfectly well. The problem 
> is that I can't (?) use performSelectorOnMainThread: because I'm not 
> an NSApplication, and so I don't get the default "main" thread that 
> loops for user input...


You don't need an NSApplication; all you need is an NSRunLoop on the 
main thread.

To get one, you should structure your main thread's code like
   ... initialization ...
   [[NSRunLoop currentRunLoop] run];
   ... teardown ...

The runloop call will cause the thread to block and repeatedly wait 
for and then handle events, until there are no more runloop sources. 
Runloop sources are things like NSTimers and the various -
performSelector... utilities declared in NSRunLoop and NSThread 
("after delay", "on main thread", etc.)

If you know exactly when to quit and don't want to deal with removing 
all runloop sources, you can just call exit(0) at the point where you 
want to exit (after any necessary cleanup.)

—Jens

Related mailsAuthorDate
mlUsing NSThreads in command-line apps ben syverson May 15, 20:42
mlRe: Using NSThreads in command-line apps Bill Bumgarner May 15, 20:47
mlRe: Using NSThreads in command-line apps Randall Meadows May 15, 20:49
mlRe: Using NSThreads in command-line apps Uli Kusterer May 15, 20:51
mlRe: Using NSThreads in command-line apps Jens Alfke May 15, 20:52
mlRe: Using NSThreads in command-line apps ben syverson May 15, 21:16
mlRe: Using NSThreads in command-line apps ben syverson May 15, 21:17
mlRe: Using NSThreads in command-line apps Bill Bumgarner May 15, 21:25
mlRe: Using NSThreads in command-line apps Bill Bumgarner May 15, 21:31
mlRe: Using NSThreads in command-line apps ben syverson May 15, 21:53
mlRe: Using NSThreads in command-line apps Jens Alfke May 15, 23:17
mlRe: Using NSThreads in command-line apps ben syverson May 16, 00:28
mlRe: Using NSThreads in command-line apps Sherm Pendley May 16, 00:31
mlRe: Using NSThreads in command-line apps Hamish Allan May 16, 00:33
mlRe: Using NSThreads in command-line apps ben syverson May 16, 00:40
mlRe: Using NSThreads in command-line apps Hamish Allan May 16, 01:06
mlRe: Using NSThreads in command-line apps Jens Alfke May 16, 03:02
mlRe: Using NSThreads in command-line apps ben syverson May 16, 06:50