Prematurely aborting a function executed in wait.make

Posted by Victorious on Tue 21 Jun 2016 02:14 PM — 9 posts, 32,461 views.

#0
Lets say I have something executing using the wait module. e.g

function someLoop()
wait.make(function()
...
end)
end

I want to be able to prematurely abort execution of this function from outside this function. E.g, some other trigger detects that something happens, and needs to stop this function. Is this possible?
USA Global Moderator #1
Can you set a variable outside the function and check its value on each time through the loop?
Australia Forum Administrator #2
I don't think your loop is in the right place anyway (although it is hard to tell from that snippet).

If there is some reason to loop (eg. 10 times) then you will create 10 instances of the wait.make coroutine at the same time. You should probably loop inside the coroutine, if looping is needed.

Then as Fiendish said, testing some global variable that you can set elsewhere, can be used to terminate the loop.
#3
I've thought of that, but it isn't fullproof. Consider a function that e.g, has a pause of 10 seconds. If i turned it off, and turned it on before it resumes execution, it won't see me turning it off. I want to avoid having 2 copies of this function executing simultaneously.
USA Global Moderator #4
I think you need to be more descriptive about the scenario you think would be problematic. I don't understand what you mean by "turn it off, and turn it on".
Amended on Wed 22 Jun 2016 03:11 PM by Fiendish
Australia Forum Administrator #5
You could replace a delay of 10 seconds with 10 delays of 1 second (or 20 delays of 0.5 seconds).

Between each delay test some flag that you set elsewhere if you want the function to abort.

The delays are implemented by a temporary timer in the MUSHclient timer section. Just cancelling the function itself wouldn't stop the timer.
USA Global Moderator #6
Nick Gammon said:

You could replace a delay of 10 seconds with 10 delays of 1 second (or 20 delays of 0.5 seconds).

Between each delay test some flag that you set elsewhere if you want the function to abort.

I wouldn't. There's no point. You shouldn't care whether the coroutine wakes up in ten seconds. You only need to care if it does work when it wakes up.

wait.time(10)
if flag_check() then return end

is just as good and simpler than

for i=1,10 do 
  wait.time(1)
  if flag_check() then return end
end


I think the only problem is that Victorious thinks there is a need to start a second copy of the coroutine instead of just letting it be a persistent consumer.
Amended on Thu 23 Jun 2016 04:29 AM by Fiendish
Australia Forum Administrator #7
Fiendish said:

I wouldn't. There's no point. You shouldn't care whether the coroutine wakes up in ten seconds. You only need to care if it does work when it wakes up.


I suppose I was thinking if the coroutine needed to release some resource or something before it exited.

Failing that, your suggestion is good.
#8
The suggestion for waiting in smaller increments ie xcellent, and is a perfect solution - thanks.

@Nick: That's right, you can view it as needing to release some resource.