1

I have a MFC dialog based app and have integrated Lua. The dialog has a text editor component so that the user can input a script. The dialog also has a button 'Run' which when pressed does: luaL_loadstring( luaVM,theScript); lua_pcall(luaVM,0,0,0); where luaVM is my main lua_State* . The dialog also has another button 'Stop' and I want when pressed to be able to stop the currently running script started by 'Run', but I cannot come up with an approach. Help would be greatly appreciated!

asked Jul 11, 2012 at 14:14
2
  • I don't know about MFC but if MFC allows you to handle one GUI event by hand, then you can set a count hook to do that every so often. Commented Jul 11, 2012 at 20:37
  • possible duplicate of Forcing a Lua script to exit Commented Jul 12, 2012 at 10:06

2 Answers 2

1

Lua is not intended to be used in such a fashion. Lua is inherently single-threaded (hardware CPU threads, not Lua threads). Once a script starts, there is no way to simply terminate it, unless you design that script to be able to terminate.

What you would have to do is start the user's script as a coroutine. And that coroutine would have to frequently (and manually) yield, so that you could check to see if you should stop the script. Since this is a manual process, you would either need the user to properly instrument their code or you would need to modify their script and add yield calls appropriately.

You might be able to use some debug hook to check every few seconds and yield the coroutine if you need it to stop. However, I have no idea if this would actually work, if it is legal to yield from a debug hook.

answered Jul 11, 2012 at 15:50

Comments

0

You could set a debug hook that sleeps for a bit.

See http://www.lua.org/manual/5.1/manual.html#lua_sethook

answered Jul 12, 2012 at 9:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.