Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Prematurely aborting a function executed in wait.make

Prematurely aborting a function executed in wait.make

Posting of new messages is disabled at present.

Refresh page


Posted by Victorious   (89 posts)  Bio
Date Tue 21 Jun 2016 02:14 PM (UTC)
Message
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?
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #1 on Tue 21 Jun 2016 04:41 PM (UTC)
Message
Can you set a variable outside the function and check its value on each time through the loop?

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Nick Gammon   Australia  (23,122 posts)  Bio   Forum Administrator
Date Reply #2 on Tue 21 Jun 2016 08:30 PM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Victorious   (89 posts)  Bio
Date Reply #3 on Wed 22 Jun 2016 10:46 AM (UTC)
Message
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.
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #4 on Wed 22 Jun 2016 03:07 PM (UTC)

Amended on Wed 22 Jun 2016 03:11 PM (UTC) by Fiendish

Message
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".

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Nick Gammon   Australia  (23,122 posts)  Bio   Forum Administrator
Date Reply #5 on Wed 22 Jun 2016 07:45 PM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #6 on Thu 23 Jun 2016 04:28 AM (UTC)

Amended on Thu 23 Jun 2016 04:29 AM (UTC) by Fiendish

Message
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.

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Nick Gammon   Australia  (23,122 posts)  Bio   Forum Administrator
Date Reply #7 on Thu 23 Jun 2016 04:52 AM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Victorious   (89 posts)  Bio
Date Reply #8 on Tue 28 Jun 2016 12:24 PM (UTC)
Message
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.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


22,925 views.

Posting of new messages is disabled at present.

Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.