discuss-gnustep
[Top][All Lists]
Advanced

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

Re: trying to understand the run loop


From: Richard Frith-Macdonald
Subject: Re: trying to understand the run loop
Date: Wed, 8 Mar 2006 07:08:00 +0000


On 8 Mar 2006, at 02:15, Lloyd Dupont wrote:

My application is not a GNUstep application at all.
Yet it uses Foundationand some of AppKit classes.
Now I'm trying to setup a framework so that everything works as well as possible.

Problem is, I don't have an explicit run loop, event pump in my developement environment, it's all event based. Yet I registerer ed an event handle on the application Idle event and manage to simulate a run loop with that.

I try to mimic the code below in my code and, I would welcome any comment, addition:


// pseudo code of my application, am I missing something?

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSUndoManager *undoManger = GetUndoManager();
[NSApplication shared];
[pool release];

while(event = GetEvent())
{
   pool = [[NSAutoreleasePool alloc] init];

   DoEvent(event);

GSNotifyASAP(); // no I won't use [runLoop runModeBeforeDate] it mess up my application
   GSNotifyIdle();

You don't want to be using internal API from the library ... if runModeBeforeDate is causing a problem you need to provide a bugfix (or at least sample code to allow someone to replicate the bug and fix it). Current code in svn has several mingw32 cleanups and might work fine for you anyway.

   while([undoManager groupingLevel] > 0)
       [undoManager endUndoGrouping];

   [pool release];
}


There are two approaches ...
The one using standard APIs would be something like above (polling from within windows event handling) ...

NSUndoManager *undoManger = GetUndoManager();
NSRunLoop *loop = [NSRunLoop currentRunLoop];
NSDate *past = [NSDate distantPast];
[NSApplication shared];

while (event = GetEvent())
{
   pool = [NSAutoreleasePool new];

   DoEvent(event);

   [runLoop acceptInputForMode: NSDefaultRunLoopMode beforeDate: past];

   while ([undoManager groupingLevel] > 0)
       [undoManager endUndoGrouping];

   [pool release];
}

The alternative, running from within a runloop, requires a bit more setup, with a class to handle windows message callbacks ...

@implementation MyWatcher
- (void) receivedEvent: (void*)handle type: (RunLoopEventType) extra: (void*)extra forMode: (NSString*)mode
{
  DoEvent(extra);
}
@end

NSUndoManager *undoManger = GetUndoManager();
NSRunLoop *loop = [NSRunLoop currentRunLoop];
MyWatcher *watcher = [MyWatcher new];

[loop addEvent: NULL type: ET_WINMSG watcher: watcher forMode: NSDefaultRunLoopMode];
[NSApplication shared];
[loop run];



While the above relies on a GNUsterp extension to NSRunLoop, which may be deprecated and then removed in later releases, it will at least be around for a while and will not be removed without some equivalent being put in place. However, the first approach (using only standard APIs) is probably best.





reply via email to

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