如何在 C# 中捕获 Lua 异常

发布于 2024-10-16 20:19:26 字数 522 浏览 2 评论 0原文

我正在使用名为 LuaInterface 的程序集在我的 C# 应用程序中运行 lua 代码。在 lua 执行期间,我创建了一些 WinForms &将事件处理程序(lua 方法)映射到它们。

问题是 doString(又名 runLuaCode)方法仅运行 init 例程和构造函数。这很好,也是有意的,但是 doString 函数是非阻塞的,因此函数返回时 Lua 创建的表单仍然存在。这意味着在构造函数期间未引发的任何异常(null-ref 等)都不会由 lua 错误处理进行处理,崩溃一直到我的编辑器的 wndProc - 这很可能会杀死我的编辑器并进行错误处理几乎不可能。

有没有办法创建一个新的线程/进程/应用程序域来处理它自己的WndProc,以便只有这个子任务需要处理异常?

我应该在 lua 中使用 while 循环阻止我的编辑器在 doString 处,直到表单关闭吗?

我还有什么其他选择?

非常感谢有关此事的任何建议!

I am using the an assembly named LuaInterface to run lua-code inside my C# application. During the lua execution I create some WinForms & map event handlers (lua-methods) to them.

The problem is that the doString (aka runLuaCode) method only runs the init routine and the constructors. This is fine and intended, however the doString function acts non blocking so the function returns while the Lua-created-Forms are still there. This means that any exception (null-ref and alike) which is not raised during the constructor is not handled by the lua error handling an crashes all the way up to my wndProc of my Editor - which most likely kills my editor and make error handling virtually impossible.

Is there any way to create a new Thread / Process / AppDomain that handles it's own WndProc so that only this sub-task needs to handle the exceptions?

Should I block my Editor at the doString with a while loop in lua until the forms are closed?

What other options do I have?

Any advice on this matter is greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぇ气 2024-10-23 20:19:26

又一个Lua爱好者!!最后! :) 我也在考虑在我的 .NET 应用程序中使用 Lua 进行宏脚本编写。

我不确定我明白了。我写了一些示例代码,它似乎工作正常。 DoString 周围的简单 try catch 会获取 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

我希望它有帮助。 :)

Another Lua enthusiast!! Finally! :) I am also toying with the idea to use Lua for macro scripting in my .NET apps.

I am not sure I get it. I wrote some sample code and it seems to be working ok. Simple try catch around DoString gets the LuaExceptions. DoString does block the main thread unless you explicitly create a new thread. In case of a new thread normal .NET multithreaded exception handling rules apply.

Example:

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 has a pretty good documentation where tricky error handling is explained.

http://penlight.luaforge.net/packages/LuaInterface/#T6

I hope it helps. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文