Execute function with timeout

Discussion in 'Mac Programming' started by i.yalovecky, Jun 9, 2010.

  1. i.yalovecky macrumors member

    Joined:
    Feb 24, 2010
    #1
    Is there something like
    Code:
    performSelector: withTimeout:
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    To the best of my knowledge no. The results would be unpredictable at best. What if the selector was altering some structure in memory and was terminated leaving the structure in an unusable state?
     
  3. i.yalovecky thread starter macrumors member

    Joined:
    Feb 24, 2010
    #3
    Hm... I work with iTunes through ScriptBridge, and sometimes iTunes freezes because of showing alert or something else. How i can terminate that task? Where i need to looking for?
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  5. i.yalovecky thread starter macrumors member

    Joined:
    Feb 24, 2010
    #5
    it was
    Code:
    	iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
    
    it yes))
    Code:
    	iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
    	iTunes.timeout = 5;
    
    found it 5 secs ago
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    That timeout feature is not part of the language, it's part of Apple Events. The value you set is most likely getting sent to the AESendMessage() function as its last parameter.

    Also a timeout of 5 is too small if iTunes is noticeably hanging. You should look up the # of ticks per second and calculate it off that.
     
  7. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #7
    Here's an idea I got working last week. This is to be considered pseudo code as I grabbed only portions of mine and added a little generic logic.

    Note that I didn't have to worry about threading in my case, or blocking, as my object is also called via a timer. The valueImWatching gets altered via another method that gets triggered via the timer.


    Code:
    @interface MyObject : NSObject {
    	NSDate * requestTimeOutDate;
    	NSInteger timeOutLength;
            id someObject;
            id valueImWatching;
    }
    
    
    @implementation MyObject
    
    -(void) timeOut: someObject {
    	if ([[NSDate date] timeIntervalSinceDate: requestTimeOutDate] > timeOutLength)
    	{
    		NSLog(@"timed out");
    		// do something, or nothing.
    	else
    	{
                    // attempt the action I'm looking for.
    		if (valueImWatching) {
    		    // do something or just exit.
    		} else {
    		    NSLog(@"call again");
    		    [NSObject cancelPreviousPerformRequestsWithTarget:self];  //selector:_cmd object: someObject];
    		    [self performSelector:_cmd withObject: someObject afterDelay:1.0];
    		    }
    	}
    }
    
    
    - (void) myFirstMethod {
            valueImWatching = nil;
            someObject = [SomeObject new]; //set it up appropriately.
    	requestTimeOutDate = [[NSDate date] retain]; //now
    	timeOutLength = 5; // seconds of timeout
    	[self timeOut: someObject];
    }
    
     

Share This Page