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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:45:10+00:00 2026-05-27T02:45:10+00:00

From my main thread I call a selector using [self performSelectorInBackground:@selector(startTask) withObject:nil]; This is

  • 0

From my main thread I call a selector using

[self performSelectorInBackground:@selector(startTask) withObject:nil];

This is the method startTask:

-(void)startTask{    
   NSTask *task = [[NSTask alloc] init];
   NSPipe *pipe = [[NSPipe alloc] init];
   NSFileHandle *fh = [pipe fileHandleForReading];

   NSArray *args = [NSArray arrayWithObjects:@"-z",iPAddress, [NSString stringWithFormat:@"%@",portNumber], nil];

   [task setLaunchPath:@"/usr/bin/nc"];
   [task setArguments:args];
   [task setStandardOutput:pipe];

   NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
   [nc removeObserver:self];
   [nc addObserver:self
          selector:@selector(dataReady:)
              name:NSFileHandleReadCompletionNotification
            object:fh];

   [task launch];
   [fh readInBackgroundAndNotify];
}

This should prevent NSTask from blocking the main thread (and the UI). But it doesn’t.
If I remove

[task launch];

The main thread doesn’t get blocked. What am I doing wrong? o_O

(BTW dataReady just handles the data. It’s not this method, that blocks…)

EDIT: I just found out, that I am not calling the selector from the main thread. I call it from a separate thread! Unfortunately I have to call it from that thread.

  • 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-27T02:45:10+00:00Added an answer on May 27, 2026 at 2:45 am

    I don’t know if this is the answer to your question, but you do have a fundamental issue:

    The docs for readInBackgroundAndNotify say:

    You must call this method from a thread that has an active run loop.

    You are not doing that because startTask is on its own thread and you are not running a run loop on it.

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

Sidebar

Related Questions

The reference says this: You should call this function from the main thread of
I call an asynchronous method from a main thread. How to kill or stop
I have the main thread from which I start a window using invokeLater .
I have called a method using a secondary thread. From inside the method i
From my main thread, I launch an image loader method method-A (below). The problem
I have a method which I am calling from onCreate( main thread). Inside the
What should I do to call thread's pause() method from showExitDialog() here ? Here's
Apart from main thread, I've ThinkerThread object _thinker whose finished() signal is connected to
I've inherited code where BeginInvoke is called from the main thread (not a background
Given a System.Timers.Timer, is there a way from the main thread to tell if

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.