I have some class A. In this class i have a method,
which calls [`self performSelectorInBackground:...]`. And it starts downloading
some info from internet.

After i tap Home button, then enter the app again, this background method keeps working.
So, if i call this method again, i have bad\_access, because background method is already working and i call it twice.

Can i stop performing selector in background of the class A? For example in my applicationDidEnterBackground?
Or can i check, if selector is performing or something?

I found couple things like

```
[[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget:a];
[NSObject cancelPreviousPerformRequestsWithTarget:a selector:@selector(startDownload) object:nil];

```

But they didn't work for me.
So

my objAppDelegate:

```
@inteface ObjAppDelegate
{
     A *a;
}
@implementation ObjAppDelegate
{
    -(void)applicationDidEnterBackground:(UIApplication *)application
    {
        //or it can be didBecomeActive..
        //here. check if background task of class A is running, or just stop it ??
    }
}

@implementation A
{
     //some timer, or event, etc.
     -(void)startDownload
     {
          [self performSelectorInBackground:@selector(runBackgroundTask)           withObject:nil];
      }
    -(void)runBackgroundTask
    {
         //some network stuff..
     }
}

```

i did it like this:

````

threadForDownload = [[NSThread alloc] initWithTarget:self selector:@selector(threadMain:) object:nil];
            [threadForDownload start];
            [self performSelector:@selector(startDownload) onThread:threadForDownload withObject:nil waitUntilDone:NO];
````

* (void)threadMain:(id)data {
NSAutoreleasePool \*pool = [NSAutoreleasePool new];

NSRunLoop \*runloop = [NSRunLoop currentRunLoop];
[runloop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];

while (YES) { 
 [runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}

[pool release];
}

In my startDownload method i look at activity indicator to check, whether
startDownload is already running..

````

-(void)startDownload
{
       if (![[UIApplication sharedApplication] isNetworkActivityIndicatorVisible])  // flag..
    {
            //....
         }
}

````

// I make visible networkActivityIndicator every time i start downloading