How to stop a function call if its take more than 3 secs in Object c - objective-c

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.

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)

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

Related

Is it guaranteed that only one callback is executed when the wait condition is true?

This is regarding the statement WAIT FOR ASYNCHRONOUS TASKS and the corresponding part of the documentation:
If the result of log_exp is false and there is an asynchronous function call with callback routine, the program waits until a callback routine of a previous function (called asynchronously) has been executed and then checks the logical expression again:
Let's say I spawn 4 tasks, each reducing availability attribute by one, reaching 0. In the callback, they increase the availability attribute by one.
Now when I reach WAIT FOR ASYNCHRONOUS TASKS UNTIL availability > 0 UP TO 6000 SECONDS. the program waits until the counter is increased by a callback.
Question: When the logical expression is checked again, is it guaranteed that the order is
callback->check->callback->check?
Or could it be that availability is e.g. already 3, since it did
callback->callback->callback->check?
It works as per documentation.
WAIT -> CALLBACK -> CHECK , WAIT -> CALLBACK -> CHECK,
until wait condition is true or no more outstanding Callbacks are open.
It is important that the Callback form/method has finished before the check is performed as that routine is responsible for changing the variable/s in the WAIT UNTIL Condition.
An extract from the docu.
If the result of log_exp is false and there is an asynchronous
function call with callback routine, the program waits until a
callback routine of a previous function (called asynchronously) has
been executed and then checks the logical expression again:
If you are concerned about 2 callbacks occurring concurrently,
the callbacks are handled by the kernel sequentially.
There is no guarantee of order, just that the call backs are processed sequentially. Note the waiting program is only executed in 1 Work process at a time. From my tests, it is always the same Work process.

Run function every 2 milliseconds

I am trying to run a function every 2 milliseconds but setting a timer to 2 milliseconds its not working it looks like it works every 50 milliseconds or so.. and when I try to use a While loop with Date.UtcNow.Ticks to compare 2 milliseconds then the CPU goes high. What options do I have here?
Depending on OS and Hardware configuration, system will allocate time slot to each process/thread,
You can try with on threading which will run full time and put statement thread.sleep(2); for delay and then run your code again in infinite while loop.

Sleep in Smalltalk Squeak

I'm dealing with N*N queens problem and gui of it.
I want to sleep for a few seconds each move so the viewer can see the process.
How do I put smalltalk to sleep?
Thank you
Instead of sleeping you can just wait.
5 seconds asDelay wait.
e.g. if you select and print it the following, it will wait 5 seconds before printing the result (2)
[
5 seconds asDelay wait.
1 + 1
] value
The comment of the Delay class explains what it does.
I am the main way that a process may pause for some amount of time. The simplest usage is like this:
(Delay forSeconds: 5) wait.
An instance of Delay responds to the message 'wait' by suspending the caller's process for a certain amount of time. The duration of the pause is specified when the Delay is created with the message forMilliseconds: or forSeconds:. A Delay can be used again when the current wait has finished. For example, a clock process might repeatedly wait on a one-second Delay.
A delay in progress when an image snapshot is saved is resumed when the snapshot is re-started. Delays work across millisecond clock roll-overs.
For a more complex example, see #testDelayOf:for:rect: .
Update: (based on comment)
wait will pause the execution flow, which means that in the example earlier, the 1 + 1 will get executed (execution flow resumed) only after the wait period has ended.
So in your class you can have...
MyBoard>>doStep
self drawBoard.
5 seconds asDelay wait.
self solve.
5 seconds asDelay wait.
self destroyBoard.

What is the difference between Thread.Sleep() and selenium.setSpeed("2000")?

What is the difference between Thread.Sleep() and selenium.setSpeed("2000")?
setSpeed : Set execution speed (i.e., set the millisecond length of a delay which will follow each selenium operation). By default, there is no such delay, i.e., the delay is 0 milliseconds.
Thread.sleep : It causes the current thread to suspend execution for a specified period.
So the main difference between them is setSpeed sets a speed while will apply delay time before every selenium operation takes place. But one thread.sleep() will set up wait only for once. So, if we have 3 selenium operations written like below:
Opeartion 1
Opeartion 2
Opeartion 3
and we want to set a delay time 2000 for each of these, defining setSpeed() method once will accomplish the task something like below:
selenium.setSpeed("2000");
Opeartion 1
Opeartion 2
Opeartion 3
But if we use Thread.sleep(), it will be something like below:
Thread.sleep(2000);
Opeartion 1
Thread.sleep(2000);
Opeartion 2
Thread.sleep(2000);
Opeartion 3
Thread.sleep() will stop the current (java) thread for the specified amount of time. It's done only once.
Selenium.setSpeed() will stop the execution for the specified amount of time for every selenium command. It is useful for demonstration purpose (you will see thing moving in your browser) or if you are using a slow web application (there are better technique to handle slow applications but that's off topic.)

Will performSelector:withObject:afterDelay: work with times under 1 sec?

Although you can pass sub-second times to performSelector:withObject:afterDelay:, it appears that the timer will fire as quickly as it can for any delay under 1 sec. For example, if I set the delay to 100 msec (0.100) or 10 msec (0.010), the timer will still fire in 2 or 3 msec. Is this a known limitation?
For performSelection:withObject:afterDelay:, the documentation for the delay reads:
delay — The minimum time before which the message is sent. Specifying a delay of 0 does not necessarily cause the selector to be performed immediately. The selector is still queued on the thread’s run loop and performed as soon as possible.
Compare this to NSTimer, where the documentation reads:
seconds — The number of seconds between firings of the timer. If seconds is less than or equal to 0.0, this method chooses the nonnegative value of 0.1 milliseconds instead.
It appears that performSelector:withObject:afterDelay: uses its delay setting just like NSTimer's seconds setting when a negative value is provided.
Can anyone confirm that that is correct?
As a follow-up, I discovered that performSelector:withObject:afterDelay: was working just fine, and that it wasn't triggering at sub-second intervals because I was passing it an int delay as follows:
int delay = 0.025; // 25 msec
[self performSelector:#selector(blahBlah:) withObject:nil afterDelay:delay];
OK, my bad! However, this leads to another observation — I thought the compiler would have reported a "loss of precision" when converting a double to an int without an explicit cast. However, it does not. Beware!
if you set the delay to 100 msec (0.100) using performSelector:withObject:afterDelay:, it will NOT be fired in 2 or 3 msec. It will be scheduled on the runloop after 100 msec, and wait until the runloop has the chance to perform. So it may be fired 102 or 103 msec after.