views:

162

answers:

3

Hi, all! I want to create multiple threads in my application. I' using following code for creating a thread. This' buttonPress method where I'm creating a thread:

  • (void) threadButtonPressed:(UIButton *)sender {

    threadStartButton.hidden = YES;

    threadValueLabel.text = @"0";
    

    threadProgressView.progress = 0.0;

    [NSThread detachNewThreadSelector:@selector(startMethod) toTarget:self withObject:nil];

}

This' where I'm calling the method for the thread:

  • (void)startMethod {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [self performSelectorOnMainThread:@selector(threadMethod) withObject:nil waitUntilDone:NO];

    [pool release];

}

  • (void)threadMethod {

    float actual = [threadProgressView progress];

    threadValueLabel.text = [NSString stringWithFormat:@"%.2f", actual];
    

    if (actual < 1) {

    threadProgressView.progress = actual + 0.01;
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(makeMyProgressBarMoving) userInfo:nil repeats:NO];
    

    }

    else 
    threadStartButton.hidden = NO;
    

    }

This thread works properly. But when I try to create another thread in the same class using the same method, it gets created properly, but at the method "performSelectorOnMainThread", it doesn't execute that method. Can anybody please help me?

A: 

I'm missing the context here. I see a call that creates a new thread, and then I see a call that performs a selector (calls a method) on the main thread..

As I understand, you are calling a function in a new thread (entryMethod), in which you call a method to perform on the main thread (myMethod). I do not understand the point of this, without some background info and possibly some code.

Is it possible that the main thread is busy performing the 'myMethod' function, and thus does not respond to other calls?

MiRAGe
Please check my edited question!
neha
A: 

It seems that you are trying to queue up methods to be executed on the main thread. You might want to look into an NSOperationQueue and NSOperation objects. If you want to proceed on this path, you might consider changing the repeats parameter to YES. The problem seems to be that the main thread is busy when it's being passed this message. This will cause the main thread to block. You may also consider not using a second threadMethod and calling back to the main thread, but instead wrapping the contents of threadMethod in an @synchronized(self) block. This way, you get the benefits of multi-threading (multiple pieces of code executing at the same time and thus a reactive user interface) without doing some weird stuff with the main thread.

RC Howe
Thanx RC Howe... That helped!
neha
A: 

Why cant you do it with the same call, by replacing

     -(void)startMethod {

      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

      [self performSelectorOnMainThread:@selector(threadMethod) withObject:nil waitUntilDone:NO];

      [pool release];

}

with

 -(void)startMethod {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];


  float actual = [threadProgressView progress];

  threadValueLabel.text = [NSString stringWithFormat:@"%.2f", actual];

  if (actual < 1) {

  threadProgressView.progress = actual + 0.01;
  [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(makeMyProgressBarMoving) userInfo:nil repeats:NO];

  }

  else 
  threadStartButton.hidden = NO;

  }

  [pool release];

}

Nithin
Since I' new to threading, I thought that specifying performSelectorOnMainThread is neccessary. Is it not?
neha
by specifying that your code is going back to the main thread, so i think, you might not be getting all the benefits of threading by that way.
Nithin