- Registriert
- 31.01.07
- Beiträge
- 388
Ich hoffe mir kann jemand bei meinem Probel helfen.
Ich möchte das in einen Status Menu eine Wert regelmäßig erneuert wird. Ich habe dafür einen Worker-Thread der einen String in einer Schleife ändert. ein Menu Item ist an den String gebunden (Binding: Controller.foo).
Das funktioniert soweit auch. Das Problem ist nur, dass ich das Programm aufhängt wenn das Menu geöffnet ist und der String (foo) geändert wird.
Wie kann ich das verhindern?
Controller.h
Controller.m
Console:
Ich möchte das in einen Status Menu eine Wert regelmäßig erneuert wird. Ich habe dafür einen Worker-Thread der einen String in einer Schleife ändert. ein Menu Item ist an den String gebunden (Binding: Controller.foo).
Das funktioniert soweit auch. Das Problem ist nur, dass ich das Programm aufhängt wenn das Menu geöffnet ist und der String (foo) geändert wird.
Wie kann ich das verhindern?
Controller.h
Code:
#import <Cocoa/Cocoa.h>
@interface Controller : NSObject {
IBOutlet NSMenu *mainMenu;
NSStatusItem *mainItem;
NSString* foo;
}
- (NSString*) foo;
- (void) setFoo: (NSString*)x;
+ (void) count: (id)param;
@end
Code:
#import "Controller.h"
@implementation Controller
- (void) awakeFromNib {
mainItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:-1] retain];
[mainItem setTitle:@"test"];
[mainItem setMenu:mainMenu];
[mainItem setHighlightMode:YES];
[NSThread detachNewThreadSelector:@selector(count:) toTarget:[Controller class] withObject:self];
}
- (NSString *) foo {
NSLog(@"Get foo: %@", foo);
return foo;
}
- (void) setFoo: (NSString *)x {
NSLog(@"Set foo: %@", x);
foo = x;
}
+(void)count:(id)param {
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
int i = 0;
while (i < 100) {
[(Controller*)param setFoo: [NSString stringWithFormat:@"foo: %d", i++] ];
sleep(1);
}
[autoreleasepool release];
}
@end
Code:
[Session started at 2009-07-10 13:02:15 +0200.]
2009-07-10 13:02:16.176 MenuTest[7700:10b] Get foo: (null)
2009-07-10 13:02:16.198 MenuTest[7700:390b] Set foo: foo: 0
2009-07-10 13:02:16.201 MenuTest[7700:390b] Get foo: foo: 0
2009-07-10 13:02:17.202 MenuTest[7700:390b] Set foo: foo: 1
2009-07-10 13:02:17.213 MenuTest[7700:390b] Get foo: foo: 1
2009-07-10 13:02:18.214 MenuTest[7700:390b] Set foo: foo: 2
2009-07-10 13:02:18.214 MenuTest[7700:390b] Get foo: foo: 2
[Session started at 2009-07-10 13:02:18 +0200.]
Loading program into debugger…
GNU gdb 6.3.50-20050815 (Apple version gdb-962) (Sat Jul 26 08:14:40 UTC 2008)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".Program loaded.
sharedlibrary apply-load-rules all
Attaching to program: `/Users/josa/Desktop/MenuTest/build/Debug/MenuTest.app/Contents/MacOS/MenuTest', process 7700.
[Switching to process 7700 thread 0x390b]
kill