0

As long as the application is running i need to change the status icon once in 2 sec., i think in my case i have to do it in separate thread.

This is my scenario.

I have 2 NSObject initialised in my MainMenu.nib. One of them is main controller AppDelegate and other one ChangeStatusBar to change the icon. I have an IBOutlet in my AppDeligate pointing to ChangeStatusBar object. From AppDeligate i am calling one method of ChangeStatusBar which uses performSelectorInBackground with run loop at achieve this. Which is not working. My method to change icon is getting called but image is not changing during AppDeligate wait. My Appdeligate can go on unconditional wait, even in such case i want StatusItem image changing. Ex: say you have scanf in AppDeligate or you are calling an API that can return after sum time. Thanks in Advance. Here is my code.

//  AppDelegate.m
-(void)applicationDidFinishLaunching:(NSNotification*)notification
{
[statusItem initStatusItem:true];  //StatusItem is the IBOutlet to ChangeStatusBar object
}

//ChangeStatusBar.m
-(void)initStatusItem:(BOOL) flag
{
   statusItem = [[[NSStatusBar systemStatusBar]
   statusItemWithLength:NSVariableStatusItemLength]retain] ;
   [statusItem setMenu:statusMenu];
   [statusItem setImage:[NSImage imageNamed:@"lk0.png"]];
   [self performSelectorInBackground:@selector(timerStart) withObject:nil];
}

-(void) timerStart
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
 [[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(changeImages)
 userInfo:nil repeats:YES] retain];
 [runLoop run];
 [pool release];
}

///Change image here
-(void)changeImages
{
  [statusItem setImage:[NSImage imageNamed:[NSString      stringWithFormat:@"lk%d.png",NoImages]]];`  
}
user12345
  • 425
  • 1
  • 3
  • 14
  • 3
    Try to embed Your call to run on main thread since it's related to GUI. Something like this: `dispatch_async(dispatch_get_main_queue(), ^{ [statusItem setImage:[NSImage imageNamed:[NSString stringWithFormat:@"lk%d.png",NoImages]]]; };` – Mateusz Szlosek Apr 10 '14 at 18:24
  • I added this in my changesImages method. Still the same result. If the main thread is running it works fine. But if you add scanf in applicationDidFinishLaunching method,images don't change. – user12345 Apr 11 '14 at 05:02
  • 1
    the code in timerStart function is running in a background thread try moving that in main thread. – bikram990 Apr 14 '14 at 08:18

0 Answers0