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 7943183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:11:38+00:00 2026-06-04T00:11:38+00:00

In other words, if I have a process that continuously runs, but users can

  • 0

In other words, if I have a process that continuously runs, but users can change parameters on the GUI that effect the process operation characteristics, where is a better place to put the process, in a NSThread or NSTimer?

  • 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-06-04T00:11:39+00:00Added an answer on June 4, 2026 at 12:11 am

    While NSThread and NSTimer are two separate things for different needs, lets compare the two functions:

    Using NSThread:

    -(void) doSomethingEverySecond {
         __block int cumValue = 0; // cumulative value
         __block void(^execBlock)() = ^{        
            while (1)
            {
                @try 
                {
                    // some code here that might either A: call continue to continue the loop, 
                    // or B: throw an exception.
                    cumValue++;
                    NSLog(@"Cumulative Value is: %i", cumValue);
    
                    if (cumValue == 5)
                        return;
                }
                @finally 
                {
                    [NSThread sleepForTimeInterval:1];
                }
            }
        };
    
        [NSThread detachNewThreadSelector:@selector(invoke) toTarget:[execBlock copy] withObject:nil];
    }
    

    Using NSTimer:

    -(void) doSomethingEverySecond {
        __block NSTimer *timer = nil;
        __block int cumValue = 0;
        __block void (^execBlock)() = ^{
            cumValue++;
            NSLog(@"Cumulative Value is: %i", cumValue);
    
            if (cumValue == 5)
                [timer invalidate];
        };
    
        timer = [NSTimer scheduledTimerWithTimeInterval:1 target:[execBlock copy] selector:@selector(invoke) userInfo:nil repeats:YES];
    }
    

    Now, if we want to something only once, NSThread is the way to go, as shown in the following:

    -(void) doSomethingOnce {    
        __block void (^execBlock)() = ^{
            NSLog(@"Doing something that could take a LONG time!");
        };
    
        [NSThread detachNewThreadSelector:@selector(invoke) toTarget:[execBlock copy] withObject:nil];
    }
    

    Now, for the NSTimer variant:

    -(void) doSomethingOnce {    
        __block void (^execBlock)() = ^{
            NSLog(@"Doing something that could take a LONG time!");
        };
    
        [NSTimer scheduledTimerWithTimeInterval:0 target:[execBlock copy] selector:@selector(invoke) userInfo:nil repeats:NO];
    }
    

    The reason for this is that we have complete control over the thread while using a NSThread, but if using a NSTimer, than we are executing inside a NSRunLoop which may freeze the UI if any heavy lifting is done inside. THAT is the advantage of a NSThread over a NSTimer.

    You are also guaranteed that a NSThread that is detached is executed immediately, with a NSTimer, which is based on NSRunLoop, cannot, as it may or may not be able to execute immediately.

    There is a 3rd alternative (well technically a fourth too, pthreads, but I will ignore that for now), GCD, but I would suggest you RTFM on that, as it’s too broad of a topic to cover in this post.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some login code that works well for Firefox and other browsers, but
In other words, can I do something with a volatile variable that could not
I have a service running that can invoke an external process to modify a
I want to write a program that can not close it In other words,
In other words, I have a string like: anything, escaped double-quotes: \, yep anything
Story: One of the app that i have works on python 2.4 and other
In other words, when I do nnet(...) I can use the size parameter to
I'm writing a program that will continuously process files placed into a hot folder.
I have a System.Timers.Timer object that I want to use, but I don't want
I have an ISAPI filter that runs on IIS6 or 7. When there are

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.