Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 928947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:05:30+00:00 2026-05-15T20:05:30+00:00

I’m having trouble with a simple threading example. I know my methods aren’t complete,

  • 0

I’m having trouble with a simple threading example. I know my methods aren’t complete, but as of now, I want to press the start button and have that fire off a thread that increments a counter every second. I know it’s connected in IB correctly since the NSLog tells me it gets to my timerThread method. But then it immediately jumps back to the initial myThread, without ever reaching the updateDisplay method and releases the pool, which is why I am guessing my program doesn’t actually increment a counter. I thought I would then put it in a sleep interval or something, but in the end I think I am missing the correct way to achieve this. Any thoughts would be great. Thanks!

@implementation MainController

-(id)initWithLabel:(UILabel *)label {

    if (self = [super init]) {
        countLabel = label;
        [countLabel retain];
    }
    return self;
}

-(int)count {
    return count;
}

-(void)setCount:(int) value {
    count = value;
}

-(void)updateDisplay:(NSTimer *)timer {
    NSLog(@"%s", __FUNCTION__);
    countLabel.text = [NSString stringWithFormat:@"%i", count];
    count++;
}

-(void)timerThread {
    NSLog(@"%s", __FUNCTION__);
    [NSTimer timerWithTimeInterval:1.0
                            target:self
                          selector:@selector(updateDisplay:)
                          userInfo:nil
                           repeats:YES];
    //NSNumber *threadID = [NSNumber numberWithInt:(int)threadID];

//  threadLabel = [NSString stringWithFormat:@"%@", threadID];
}

-(void)myThread {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    //NSNumber *threadID = [NSNumber numberWithInt:(int)threadID];
    [self performSelectorOnMainThread:@selector(timerThread)
                           withObject:nil
                        waitUntilDone:NO];
//  NSLog(@"threadID in myThread: %@", threadID);
    [pool release];
}

-(void)startThread {
//  threadIndex = 0;
//  numThreads = 0;
//  NSNumber *threadID = [NSNumber numberWithInt:threadIndex++];
    [self performSelectorInBackground:@selector(myThread) withObject:nil];
//  NSLog(@"%i", threadIndex);
    numThreads++;
}

-(void)myThreadStop {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [NSThread exit];
    [self performSelectorOnMainThread:@selector(updateDisplay)
                           withObject:nil 
                        waitUntilDone:NO];

    [pool release];
}

-(void)stopThread {
    [self performSelectorInBackground:@selector(myThreadStop) withObject:nil];
}

-(void) dealloc {
    [countLabel release];
    [super dealloc];
}

@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:05:30+00:00Added an answer on May 15, 2026 at 8:05 pm

    The short answer is that you did not schedule the timer.

    iOS (and others) use run loops. Each thread may have a run loop, and the main UI thread has a run loop set up for you. Simplistically, a run loop keeps a queue of things to do and either does them in an ordered fashion or blocks until there is something to do.

    Anything that you do with the active UI, like setting the text of a UILabel, must be done on the main thread. In your case, you have set up (but not scheduled) a timer on the main thread to update the timer. Scheduling a timer just means adding it to the run loop.

    If you had a lengthy task to perform that would update the UI at the end, you could use performSelectorInBackground to start the lengthy task and performSelectorOnMainThread when the task finishes to update UI.

    If you have a short periodic task, such as updating a clock or counter UI, you can just create an NSTimer on the same thread you want the timer to fire on. When creating the timer, use a scheduledTimerWithTimeInterval variant so it will start firing automatically.

    When you create a repeating timer, you must keep a reference to it so you can invalidate the timer before the target is released. At the latest, in dealloc you should invalidate the timer.

    Instead of calling startThread, turn timerThread into startTimer.

    -(void) startTimer {
        timerMember = [[NSTimer
          scheduledTimerWithTimeInterval:1.0
                                  target:self
                                selector:@selector(updateDisplay:)
                                userInfo:nil
                                 repeats:YES] retain];
    }
    
    -(void) dealloc {
         [timerMember invalidate];
         [timerMember release];
         ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.