• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to set up a thread listening to performSelector: messages?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to set up a thread listening to performSelector: messages?


  • Subject: Re: How to set up a thread listening to performSelector: messages?
  • From: "Oleg Krupnov" <email@hidden>
  • Date: Thu, 25 Sep 2008 18:53:43 +0300

I actually have tried this. My code looks like this (is it correct?):

@implementation Worker

- (void)threadMain:(id)data
{
  runLoop = [NSRunLoop currentRunLoop];
  [runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
  while(true)
  {
        [runLoop run];
  }
}

- (void)processRequest:(id)sender
{
  NSLog(@"hello");
}

@end

Then from the main thread I send:

[[worker runLoop] performSelector:@selector(processRequest:)
target:worker argument:request order:0 modes:[NSArray
arrayWithObject:NSDefaultRunLoopMode]];

This time the -[NSRunLoop run] method does not exit immediately.
However the problem is that processRequest is never called and the
-[NSRunLoop run] never exits.

What can be wrong? Should it work like this at all?


On Thu, Sep 25, 2008 at 6:46 PM, Ken Thomases <email@hidden> wrote:
> On Sep 25, 2008, at 10:30 AM, Oleg Krupnov wrote:
>
>> I want to create a secondary thread with its run loop running
>> (sleeping) until a performSelector is sent to it. Then it wakes up,
>> performs the selector, and go to sleep again. I imagine it to be
>> something like this:
>>
>> - (void)threadMain:(id)data
>> {
>>   while(true)
>>   {
>>        [[NSRunLoop currentRunLoop] run];
>>   }
>> }
>>
>> However, the -[NSRunLoop run] method exits immediately, because, in
>> accord with the the docs, there are no input sources or timers
>> attached to the run loop.
>>
>> So I need to set up an input source, right?. The docs say input
>> sources can be port-based or custom *OR* just performSelector calls. I
>> am interested only in the performSelector calls.
>>
>> So what should my code look like to set up such input source?
>
> You can just create an NSPort and add it to the run loop.  It doesn't matter
> that you won't actually be using the port.
>
> Cheers,
> Ken
>
>
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: How to set up a thread listening to performSelector: messages?
      • From: Jean-Daniel Dupas <email@hidden>
    • Re: How to set up a thread listening to performSelector: messages?
      • From: Ken Thomases <email@hidden>
    • Re: How to set up a thread listening to performSelector: messages?
      • From: Dave Dribin <email@hidden>
    • Re: How to set up a thread listening to performSelector: messages?
      • From: "Michael Ash" <email@hidden>
References: 
 >How to set up a thread listening to performSelector: messages? (From: "Oleg Krupnov" <email@hidden>)
 >Re: How to set up a thread listening to performSelector: messages? (From: Ken Thomases <email@hidden>)

  • Prev by Date: Re: How to set up a thread listening to performSelector: messages?
  • Next by Date: Re: What does @loader_path refer to when loading ibplugins from a linked-in framework?
  • Previous by thread: Re: How to set up a thread listening to performSelector: messages?
  • Next by thread: Re: How to set up a thread listening to performSelector: messages?
  • Index(es):
    • Date
    • Thread