Threads and GUI
Threads and GUI
- Subject: Threads and GUI
- From: kubernan <email@hidden>
- Date: Thu, 3 Jan 2002 21:42:16 +0100
hello all and happy new year !
I have a problem with my multi-threaded application and its GUI.
I have a window with a NSDrawer. The opening of this one depends on a
flag used in a detached thread. Using a NSTimer i call a method that
test this flag and open the NSDrawer if necessary.
The problem is doing this my app becomes very slow.
Here's a sample code :
- (IBAction)launchAction:(id)sender
{
// some init
tictac = [[NSTimer timerWithTimeInterval:0.0 // 0.0 for "real time"
target:self
selector:@selector(test)
userInfo:nil
repeats:YES] retain];
[[NSRunLoop currentRunLoop] addTimer:tictac
forMode:NSDefaultRunLoopMode];
[NSThread detachNewThreadSelector:@selector(createLocalThread)
toTarget:self withObject:nil];
}
-(void)createLocalThread
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init ];
// do some work in a loop and under condition :
theFlag = TRUE;
// continue some work
[pool release];
[NSThread exit];
}
-(void)test
{
// some init
if (theFlag)
{
[myDrawer open]
[tictac invalidate];
}
}
It works but it's not a nice choice : test method is called even if i
don't need to open the drawer (that's why i use a flag)....and it's very
slow.
Instead of using NSTimer i tried with NSNotificationCenter :
-(void)createLocalThread
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init ];
// some init
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(openTheDrawer:)
name:@"openDrawer" object:nil];
// do some work in a loop and under condition :
[[NSNotificationCenter defaultCenter]
postNotificationName:@"openDrawer" object:nil];
// continue some work
[pool release];
[NSThread exit];
}
- (void)openTheDrawer:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:@"openDrawer" object:nil];
[myDrawer open];
NSLog(@"open the drawer);
}
Here it's really fast but even if if i have the text "open the drawer"
in the log, myDrawer doesn't appear ! Trying to understand why, i put
some resize window code in openTheDrawer function. I had strange
results : the main window is resized but shadow effect of this one
doesn't appear and if i place the open order of myDrawer before resizing
the window, i can only see an blank opened drawer (the view
is...somewhere).
If i replace the postNotification (in createLocalThread) directly by
[myDrawer open] then the drawer appears good.
I tried to use NSLock and flushWindow for the shadow pb without success.
Can you explain to me why i have these problems ? Do you have any
solution for me ?
Thx a lot.
Kub.