bug-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: run NSApllication in an other thread


From: Richard Frith-Macdonald
Subject: Re: run NSApllication in an other thread
Date: Tue, 17 Feb 2009 21:08:59 +0000


On 17 Feb 2009, at 20:36, Julien Isorce wrote:

Hi,

I can read in NSApplication's documentation that it must be called in the main thread.

I am in a case where there is an other main loop (in C) that I cannot managed. The code I can access is an interface and its implementation that leads to a shared library loaded by the main application where there is this C main loop. Well, in the implementation (that I have to make) threre is 3 functions. One for initialization, one for deinitialization, and the last one is called periodically. In the init function I create an NSWindow. Then this window has to be updated in the function called periodically.

In no cocoa env I usually create a thread in the init function. This thread runs the specific window main loop (X loop or gdi32 loop). And it works pretty good. I want to do the same thing in a cocoa env. I mean, i would like to run the cocoa main loop in a thread created in the init function.

Even if I read that NSApplicationMain (or [NSApp run] ) must be called in the main thread, I tried to call it in a different thread.
But I am getting an acces violation in the main thread.
Then if a replace the [NSApp run] from the second thread, by a while an a sleep, I have an access violation in the main thread only I do not call [NSApp run] in the main thread. (but I cannot call [NSApp run] in the main thread because the function must be called periodically, I must not block the main thread)

I am creating the thread with:

 [NSThread detachNewThreadSelector: @selector(start:)
        toTarget: [AppThread class]
        withObject: nil];

(like I could see in the gnu step examples)

I am still on win32.

Any suggestion ?

I wouldn't do it that way round.
Why not create an object to manage the C event loop, and run that in a secondary thread.

eg.

@interface LoopHandler : NSObject
- (void) callback;
- (void) run;
@end

@implementation LoopHandler

static LoopHandler      *instance = nil;

static void
periodicLoopCallback()
{
[instance performSelectorInMainThread: @selector(callback) withObject: nil waitUntilDone: YES];
}

- (void) callback
{
  /* perform callback handling here.
   * this will be executed in the main thread whenever the
   * C loop in the secondary thread calls preiodicLoopCallback
   */
}

- (id) init
{
  instance = self;
  return self;
}

- (void) run
{
// initialise C loop and tell it to use the callback function periodically
   // run the C loop
}
@end

Then you would call

[NSThread detachNewThreadSelector: @selector(run) toTarget: [LoopHandler new] withObject: nil];

in your application startup code, and have all the GNUstep stuff working normally






reply via email to

[Prev in Thread] Current Thread [Next in Thread]