[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Prematurely aborting a function executed in wait.make

Prematurely aborting a function executed in wait.make

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Victorious   (89 posts)  [Biography] 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?
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] 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
[Go to top] top

Posted by Victorious   (89 posts)  [Biography] 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.
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] 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
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] 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
[Go to top] top

Posted by Victorious   (89 posts)  [Biography] 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.
[Go to top] 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.


20,580 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]