如何在 C# 中捕获 Lua 异常
我正在使用名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
又一个Lua爱好者!!最后! :) 我也在考虑在我的 .NET 应用程序中使用 Lua 进行宏脚本编写。
我不确定我明白了。我写了一些示例代码,它似乎工作正常。 DoString 周围的简单 try catch 会获取 LuaExceptions。 DoString 确实会阻塞主线程,除非您显式创建一个新线程。如果是新线程,则应用正常的 .NET 多线程异常处理规则。
示例:
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:
LuaInterface has a pretty good documentation where tricky error handling is explained.
http://penlight.luaforge.net/packages/LuaInterface/#T6
I hope it helps. :)