我正在尝试在我的 C# 应用程序上使用 lua 脚本,但是在运行不同的脚本时遇到问题,当我运行 3~4 个脚本时,我不断收到此异常:
如您所见,我正在“捕获” AccessViolationException 异常,以防万一:
[HandleProcessCorruptedStateExceptions]
public void ExecuteAsync(MyLuaThread thread)
{
Task.Factory.StartNew(() =>
{
if (Monitor.TryEnter(ObjLocker, new TimeSpan(0, 0, 5)))
{
try
{
Thread.Sleep(10);
if (ScriptRunning[thread.name])
{
var result = lua.DoString(thread.code);
}
Thread.Sleep(10);
}
catch (AccessViolationException e4)
{
thread.errors.Add(time() + " - Access Error - " + e4.Message);
print("(" + thread.name + ") Access Error - " + e4.Message);
}
catch (NLua.Exceptions.LuaScriptException e)
{
thread.errors.Add(time() + " - Script Error - Line: " + e.Message.Substring(e.Message.IndexOf(":")));
print("(" + thread.name + ")Line: " + e.Message.Substring(e.Message.IndexOf(":")));
}
catch (Exception otherEx)
{
thread.errors.Add(time() + " - Other Error - " + otherEx.Message);
print("(" + thread.name + ") Access Error - " + otherEx.Message);
}
finally
{
Thread.Sleep(10);
Monitor.Exit(ObjLocker);
}
}
});
}
我创建了这个小项目来向你们展示正在发生的错误。只需添加 3~5 个线程并启动它们,崩溃发生的时间不应超过 5 秒。
小项目链接:https ://www.dropbox.com/s/4fyk8tblff7v5wa/SmallLuaTest.rar