每十秒检查一次后台状况并发送通知

发布于 2024-11-07 14:24:03 字数 1416 浏览 0 评论 0原文

我正在开发一个适用于 Mac OS X 的应用程序。我的应用程序每十秒检查一次,如果条件成立,该应用程序会发送一条 Growl 通知。

我已经对咆哮通知和检查进行了编码。我只需要知道如何使此检查每十秒重复一次,并且每次在后台发送通知(如果为真)。

请写出准确的代码,因为我对 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

I'm working on an app for Mac OS X. My app checks something every ten seconds, and if the condition is true, the app sends a Growl notification.

I've already coded the Growl notifications and the checking. I just need to know how to make this checking repeat itself every ten seconds, and each time send a notification if true, all in the background.

Please write the exact code, as I am very new to Objective-C. Thank you :D

-------------------------------EDIT------------------------------------

Currently i'm using this:

//  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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忱杏 2024-11-14 14:24:03

您正在寻找的确切代码可以在这里找到:
时间编程主题

The exact code that you are looking for can be found here:
Time Programming Topics

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

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

当您完成计时器调用[timer invalidate]。粘贴到 XCode 并按住 alt+单击以阅读文档。

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

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

When you are done with the timer call [timer invalidate]. Paste to XCode and alt+click it to read the docs.

很酷又爱笑 2024-11-14 14:24:03

要安排计时器每 10 秒运行一次,您需要这样做:

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

这将创建一个计时器并将其放入运行循环中,以便每 10 秒触发一次。当它触发时,它相当于以下方法调用:

[someObject fire: someParameter];

您可以将 nil 作为 someParameter 传递,在这种情况下,您的选择器不需要接受参数,即它可以是 -fire > 而不是 -fire:

要停止计时器,只需向其发送 invalidate 消息即可。

[timer invalidate];

定时器需要运行循环才能工作。如果您在应用程序的主线程上运行它,那就没问题,因为主线程已经有一个运行循环(它负责处理 UI 事件并将它们传递给您的操作)。如果您希望计时器在不同的线程上触发,则必须在该不同的线程上创建并运行一个运行循环。这有点高级,所以考虑到您是 Objective-C 的新手,现在请避免使用它。


编辑

看到您想要执行的操作后,安排计时器的第一段代码需要替换整个 while 循环。 -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...
}

To schedule a timer to run every 10 seconds, you need this:

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

That will create a timer and put it on the run loop such that it fires every 10 seconds. When it fires, it is equivalent to the following method invocation:

[someObject fire: someParameter];

You are allowed to pass nil as someParameter in which case your selector need not take a parameter i.e. it could be -fire rather than -fire:.

To stop a timer, simply send it the invalidate message.

[timer invalidate];

Timers require a run loop to work. This is fine if you run it on the main thread of your application since the main thread already has a run loop (it's what handles UI events and passes them off to your actions). If you want the timer to fire on a different thread, you have to create and run a run loop on that different thread. This is a bit more advanced, so given you are new to Objective-C avoid it for now.


Edit

Having seen what you are trying to do, the first bit of code to schedule the timer needs to replace that entire while loop. The -fire method would look something like:

-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...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文