-1

i have a function it will take 3 to 30 secs time for execution depends on some calculations.

i want to stop if my function call takes more than 5 secs. how to do this in Objective C.

user1120572
  • 771
  • 1
  • 7
  • 7

2 Answers2

2

You have to execute command in a separate thread (with performSelectorInBackground or with NSThread, for example), wait for 5 seconds (again, with unix sleep or NSThread methods) and then (depending on what is being done in the execution thread):

  • set some field in a class to "terminate", and check this field often in a "long" function
  • cancel IO operation, if there is a block
  • cancel a thread (you can read about it here: how to stop nsthread)
Community
  • 1
  • 1
bealex
  • 10,004
  • 1
  • 21
  • 27
0

You can use a timer and put a condition like if the timer exceeds 5 seconds, do nothing.

Yama
  • 2,649
  • 3
  • 31
  • 63
  • Hi i am new to objective c , i tried with timer with 5 sec , but inside the "timerTick"function i called my function but my function execute after 30 sec then timer is not exit after 5 sec, .can you please share a sample code. – user1120572 Dec 29 '11 at 08:50