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

  • Home
  • SEARCH
  • 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 6628341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:07:12+00:00 2026-05-25T22:07:12+00:00

I have a simple thread that runs an infinite loop. Eventually, the loop will

  • 0

I have a simple thread that runs an infinite loop. Eventually, the loop will check for serial data at a USB port a few thousand times per second, but at the moment, it just writes something to a custom class of NSTextView once every second.

int i;
for (i=1; i>0; i++)
{
    [lock lock];
    [[self textStorage] replaceCharactersInRange:NSMakeRange([[self textStorage] length], 0) withString:@"test\n"];
    [lock unlock];
    sleep(1);
}

The issue is that it writes really sporadically. It will do one or two, then wait ten seconds and spit ten of them out at once. If I replace the writing line with an NSLog(@"test"), it logs at nice even intervals. I have another test method in the main thread that accepts input into a text field and puts it into the text view, and doing this seems to update the text view to include the child thread’s most recent writes. There shouldn’t be anything interfering with it at this point anyway, but I’ve locked everything everywhere just to be sure. Thanks in advance.

  • 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-25T22:07:12+00:00Added an answer on May 25, 2026 at 10:07 pm

    You should always perform operations that affect the UI from the main thread. You can have the child thread create a temporary object that holds the results, and then use performSelectorOnMainThread:withObject:waitUntilDone: to call another method that will do the necessary modifications on the main thread.

    NSString * const MDResultKey = @"MDResult";
    
    - (void)someMethod {
        // 
        int i;
        for (i=1; i>0; i++) {
             // if necessary, create an object to hold results
             NSDictionary *results = [NSDictionary 
                   dictionaryWithObjectsAndKeys:@"test", MDResultKey, nil];
    
             [self performSelectorOnMainThread:@selector(updateUIWithResults:) 
                      withObject:results waitUntilDone:NO];
    
             sleep(1);
        }
    }
    
    
    - (void)updateUIWithResults:(NSDictionary *)results {
        NSString *result = [results objectForKey:MDResultKey];
        [lock lock]; // ?
        [[self textStorage] replaceCharactersInRange:
               NSMakeRange([[self textStorage] length], 0) withString:result];
        [lock unlock]; // ?
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets just say you have a simple operation that runs on a background thread.
I have simple serial port program that is supposed to read the serial port
I have a simple 'Working' form that runs on its own thread to keep
I have a simple webform that will allow unauthenticated users to input their information,
I have a simple Scala project that runs without any problems inside Eclipse, however,
I need to write a simple app that runs two threads: - thread 1:
I have a multithreaded application that runs using a custom thread pool class. The
I have a Python program that runs a series of experiments, with no data
I have a thread that runs querying a DB and returning some values. If
I have a simple console application that runs calculations in several threads (10-20 of

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.