LLThreadWorker Class Reference

List of all members.

Public Member Functions

(void) - markAsCancelled
(BOOL) - cancelled
(void) - dealloc

Static Public Member Functions

(LLThreadWorker *) + workOn:withSelector:withObject:didEndSelector:
(NSString *) + description

Protected Attributes

id _target
SEL _selector
id _argument
SEL _didEndSelector
NSConnection * _callingConnection
NSPort * _port1
NSPort * _port2
NSConnection * _conn2
NSConditionLock * _cancelled
BOOL _endRunLoop

Detailed Description

Definition at line 97 of file LLThreadWorker.h.


Member Function Documentation

- (BOOL) cancelled  

cancelled Returns whether or not someone has tried to cancel the thread. Returns whether or not someone has tried to cancel the thread.

- (void) dealloc  

dealloc Make sure we clean up after ourselves. Make sure we clean up after ourselves.

+ (NSString *) description  

description Just a little note to say, "Good job, Rob!" Just a little note to say, "Good job, Rob!" to the original author of this Public Domain software.

- (void) markAsCancelled  

markAsCancelled Mark the LLThreadWorker as cancelled. Marks the LLThreadWorker as cancelled but doesn't actually cancel the thread. It is up to you to check whether or not the LLThreadWorker is cancelled using a two-argument "longTask:..." method like so:

  • (id)longTask:(id)userInfo anyNameHere:(LLThreadWorker *)tw { ... while(... && ![tw cancelled]){ ... } }

+ (LLThreadWorker *) workOn: (id)  target
withSelector: (SEL)  selector
withObject: (id)  userInfo
didEndSelector: (SEL)  didEndSelector 

workOn:withSelector:withObject:didEndSelector:

Parameters:
target The object to receive the selector message. It is retained.
selector The selector to be called on the target in the worker thread.
userInfo An optional argument if you wish to pass one to the selector and target. It is retained.
didEndSelector An optional selector to call on the target. Use the value 0 (zero) if you don't want a selector called at the end.
Returns:
Returns an autoreleased LLThreadWorker that manages the worker thread.
Call this class method to work on something in another thread.

Example:

    NSDictionary *thingsIllNeed = [NSDictionary dictionaryWithObjectsAndKeys:
       self, @"self",
       myProgressIndicator, @"progress",
       myStatusField, @"status", nil];

    [LLThreadWorker workOn:self 
                  withSelector:@selector(longTask:) 
                  withObject:thingsIllNeed
                  didEndSelector:@selector(longTaskFinished:)];
 

The longTask method in self will then be called and should look something like this:

  • (id)longTask:(id)userInfo { Do something that takes a while and uses 'userInfo' if you want id otherSelf = [userInfo objectForKey:@"self"]; NSProgressIndicator *progress = (NSProgressIndicator *)[userInfo objectForKey:@"progress"]; NSTextField *status = (NSTextField *)[userInfo objectForKey:@"status"];

        return userInfo; // Will be passed to didEndSelector
    }    
 
Optionally you can have this "longTask" method accept a second argument which will be the controlling LLThreadWorker instance which you can use to see if the LLThreadWorker has been marked as cancelled. Your "longTask" method might then look like this:
  • (id)longTask:(id)userInfo anyNameHere:(LLThreadWorker *)tw { ... while(... && ![tw cancelled]){ ... } } You can name the second parameter anythign you want. You only have to match it when you create the LLThreadWorker like so:
        [LLThreadWorker workOn:self 
                      withSelector:@selector(longTask: anyNameHere:) 
                      withObject:userInfo
                      didEndSelector:@selector(longTaskFinished:)];

 
When your longTask method is finished, whatever is returned from it will be passed to the didEndSelector (if it's not nil) as that selector's only argument. The didEndSelector will be called on the original thread, so if you launched the thread as a result of a user clicking on something, the longTaskFinished will be called on the main thread, which is what you need if you want to then modify any GUI components. The longTaskFinished method might look something like this, then:
  • (void)longTaskFinished:(id)userInfo { Do something now that the thread is done ... } Of course you will have to have imported the LLThreadWorker.h file in your class's header file. The top of your header file might then look like this:
        import <Cocoa/Cocoa.h>
        import "LLThreadWorker.h"
     
    Enjoy.

Referenced by Communication::startCommunicationThread.


The documentation for this class was generated from the following files:
Generated on Fri Jul 28 19:15:54 2006 for MacTrek by  doxygen 1.4.7