又一个 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
我希望它有所帮助。:)