2011-05-17 69 views
1

我正在爲Mac OS X.我的應用程序檢查的東西每隔十秒一個應用程序,並且如果條件爲真,則應用程序發送一個低吼通知。檢查的條件在後臺每十秒鐘發送通知

我已經編碼的咆哮的通知和檢查。我只需要知道如何使此檢查每十秒鐘重複一次,並且每次都在後臺發送通知(如果爲true)。

請寫出確切的代碼,因爲我是很新的Objective-C的。謝謝:d

-------------------------------編輯---------- --------------------------

目前我使用此:

// MyApp_AppDelegate.m 

#import "MyApp_AppDelegate.h" 

@implementation MyApp_AppDelegate 

- (void)awakeFromNib { 
    return; 
} 


-(void)applicationDidFinishLaunching:(NSNotification*)aNotification { 

    // grwol: 
    NSBundle *myBundle = [NSBundle bundleForClass:[MyApp_AppDelegate class]]; 
    NSString *growlPath = [[myBundle privateFrameworksPath] stringByAppendingPathComponent:@"Growl-WithInstaller.framework"]; 
    NSBundle *growlBundle = [NSBundle bundleWithPath:growlPath]; 

    #include <unistd.h> 

    int x = 0; 
    int l = 10; // time/repeats 
    int t = 10; //seconds 
    while (x <= l) { 

     // more code here only to determine sendgrowl value... 

     if(sendgrowl) { 
      if (growlBundle && [growlBundle load]) { 
       // more code to sends growl 
      } else { 
       NSLog(@"ERROR: Could not load Growl.framework"); 
      } 
     } 

     // do other stuff that doesn't matter... 

     // wait: 
     sleep(t); 

     x++; 
    } 
} 

/* Dealloc method */ 
- (void) dealloc { 
    [super dealloc]; 
} 

@end 
+2

永遠不要在應用程序主線程上運行的方法中使用'sleep()'。整個用戶界面將凍結這段時間。 – JeremyP 2011-05-17 14:47:49

+0

概括:做**沒有**曾經使用'sleep()'。 – bbum 2011-05-17 15:33:23

+3

另外;考慮到您在回答問題時所提問題的基本性質,我建議您閱讀Objective-C和Cocoa入門指南。他們會幫助很多。 – bbum 2011-05-17 15:34:47

回答

1

,你是確切的代碼尋找可以在這裏找到: Time Programming Topics

+0

謝謝,但正如我所說我是新來obj-c和超過一半的代碼,我不明白,你不能只給我我需要的... – Andy 2011-05-17 13:48:00

+0

標題爲「預定定時器」是你需要閱讀來做你需要做的事情。我知道閱讀文檔可能會造成混淆,但是一旦你學會了瀏覽它們,找到你的問題的答案變得更容易。 – sosborn 2011-05-17 13:51:29

+0

@sosborn:無法正常工作... – Andy 2011-05-17 14:17:01

0
-(void) sendGrowl { } // your growl method 

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self 
        selector:@selector(sendGrowl) userInfo:nil repeats:YES]; 

當您使用的計時器調用[timer invalidate]完成。粘貼到XCode,然後點擊它來閱讀文檔。

0

要安排一個計時器,每10秒運行,你需要這樣的:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 10.0 
                target: someObject 
               selector: @selector(fire:) 
               userInfo: someParameter 
               repeats: YES]; 

這將創建一個定時器,並把它的運行循環,使得它激發每10秒。火災時,它相當於下面的方法調用:

[someObject fire: someParameter]; 

你被允許通過零爲someParameter在這種情況下,您的選擇不一定需要一個參數,即它可能是-fire而非-fire:

要停止計時,只要向它發送invalidate消息。

[timer invalidate]; 

定時器需要運行循環才能工作。如果您的應用程序,因爲主線程已經有一個運行循環(這是什麼處理UI事件,並將它們傳遞給您的操作)的主線程上運行它,這是好的。如果你想讓定時器在另一個線程上觸發,你必須在該線程上創建並運行一個運行循環。這有點高級,所以鑑於你是Objective-C的新手,現在就避免它。


編輯

在看到你正在嘗試做的,代碼安排計時器第一位需要更換,而循環,整個。 -fire方法看起來像這樣:

-fire 
{ 
    // code here only to determine sendgrowl value... 

    if(sendgrowl) 
    { 
     if (growlBundle && [growlBundle load]) 
     { 
      // more code to sends growl 
     } 
     else 
     { 
      NSLog(@"ERROR: Could not load Growl.framework"); 
     } 
    } 
    // do other stuff that doesn't matter... 
} 
+0

只是最後一個questino:我應該把'NSBundle * myBundle = [NSBundle bundleForClass:[MyApp_AppDelegate class]]放在哪裏? NSString * growlPath = [[myBundle privateFrameworksPath] stringByAppendingPathComponent:@「Growl-WithInstaller.framework」];我們可以使用下面的例子來說明一下這個例子:NSBundle * growlBundle = [NSBundle bundleWithPath:growlPath];''和NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:someObject selector:@selector(fire :) userInfo:someParameter repeatats:YES] :someParameter]'和'[timer invalidate]'... 非常感謝:) – Andy 2011-05-17 14:57:13

+0

捆綁的東西停留在它的位置。計劃定時器應該替換整個while循環。 – JeremyP 2011-05-17 15:00:12

+0

和'NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:someObject selector:@selector(fire :) :) userInfo:someParameter repeatats:YES];'捆綁後的東西? – Andy 2011-05-17 15:04:52