我使用名为LuaInterface的程序集在我的C#应用程序中运行lua-code。在lua执行期间,我创建了一些WinForms&将事件处理程序(lua-methods)映射到它们。
问题是doString
(aka runLuaCode
)方法只运行init例程和构造函数。这很好,并且有意,但是doString
函数非阻塞,因此函数返回,而Lua创建的表单仍然存在。这意味着在构造函数期间没有引发的任何异常(null-ref和相似)都不会被处理崩溃的lua错误处理到我的编辑器的wndProc - 这很可能会杀死我的编辑器并进行错误处理几乎不可能。
有没有办法创建一个新的Thread / Process / AppDomain来处理它自己的WndProc,这样只有这个子任务需要处理异常?
我是否应该在lua中使用while循环阻止我的编辑器,直到表单关闭?
我还有其他选择吗?
非常感谢有关此事的任何建议!
答案 0 :(得分:0)
另一个Lua爱好者!!最后! :)我也想在我的.NET应用程序中使用Lua进行宏脚本编写。
我不确定我明白了。我写了一些示例代码,似乎工作正常。简单的尝试捕获DoString获取LuaExceptions。除非您显式创建新线程,否则DoString会阻止主线程。如果是新线程,则适用.NET多线程异常处理规则。
示例:
public const string ScriptTxt = @"
luanet.load_assembly ""System.Windows.Forms""
luanet.load_assembly ""System.Drawing""
Form = luanet.import_type ""System.Windows.Forms.Form""
Button = luanet.import_type ""System.Windows.Forms.Button""
Point = luanet.import_type ""System.Drawing.Point""
MessageBox = luanet.import_type ""System.Windows.Forms.MessageBox""
MessageBoxButtons = luanet.import_type ""System.Windows.Forms.MessageBoxButtons""
form = Form()
form.Text = ""Hello, World!""
button = Button()
button.Text = ""Click Me!""
button.Location = Point(20,20)
button.Click:Add(function()
MessageBox:Show(""Clicked!"", """", MessageBoxButtons.OK) -- this will throw an ex
end)
form.Controls:Add(button)
form:ShowDialog()";
private static void Main(string[] args)
{
try
{
var lua = new Lua();
lua.DoString(ScriptTxt);
}
catch(LuaException ex)
{
Console.WriteLine(ex.Message);
}
catch(Exception ex)
{
if (ex.Source == "LuaInterface")
{
Console.WriteLine(ex.Message);
}
else
{
throw;
}
}
Console.ReadLine();
}
LuaInterface有一个非常好的文档,其中解释了棘手的错误处理。
http://penlight.luaforge.net/packages/LuaInterface/#T6
我希望它有所帮助。 :)