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!

user1452276
  • 91
  • 1
  • 7
  • 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. – lhf Jul 11 '12 at 20:37
  • possible duplicate of [Forcing a Lua script to exit](http://stackoverflow.com/questions/6913999/forcing-a-lua-script-to-exit) – ltjax Jul 12 '12 at 10:06

2 Answers2

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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
0

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

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

daurnimator
  • 4,091
  • 18
  • 34