NLua / NLua

Bridge between Lua and the .NET.
http://nlua.org
MIT License
2.03k stars 307 forks source link

How to kill script from another thread? #359

Open jinha3411 opened 4 years ago

jinha3411 commented 4 years ago

I currently have two thread : Monitoring thread that monitors LuaObject and flags & Execution Thread that actually runs the script.

What I am trying to achieve is to kill the running script (script that "DoFile"ed by Lua object) when the monitoring thread checks a certain flag (e.g., bool killscript = true).

I am new to Lua and having hard time to figure out.

Any hints would be much appreciated

rusamentiaga commented 4 years ago

One option:

When you want to break the execution from the monitor thread create a hook:

void SetAbortProgramHook()
{
    _lua.DebugHook += LuaAbortProgramHook;
    _lua.SetDebugHook(KeraLua.LuaHookMask.Count, 1);
}

void LuaAbortProgramHook(object sender, NLua.Event.DebugHookEventArgs args)
{
    _lua.Push("Execution aborted");
    _lua.State.Error();
}

Note: this wont work if the execution thread is locked.