LINQPad 和未处理的异常
我尝试使用以下 C# 语句使 LINQPad4 崩溃:
new Thread(() => new Thread(() => { throw new Exception(); }).Start()).Start();
显示未处理的异常对话框,但进程不会终止。我想 IsTerminate = true 就像所有 UnhandledThreadExceptions 中一样......它如何阻止进程死亡?
I'm trying to crash LINQPad4 using the following C# statement:
new Thread(() => new Thread(() => { throw new Exception(); }).Start()).Start();
An unhandled exception dialog is shown, but the process doesn't die. I suppose IsTerminating = true like in all UnhandledThreadExceptions... how does it stop the process from dying?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它也有一个针对所有非 UI 线程异常的全局异常处理程序,在 Main 方法中类似这样:
当然还有其他小事情要做,加上像往常一样围绕 Application.Run 进行 try/catch。
请参阅此处的完整文章和详细信息:C# 教程 - 处理未处理的异常
编辑: hb.尝试调试这个:;-)
it has a global exception handler for all non UI thread exceptions as well, something like this in the Main method:
there are also other small things to do of course plus as usual the try/catch around the Application.Run.
see a full article and details here: C# Tutorial - Dealing With Unhandled Exceptions
Edit: hb. try to debug this one: ;-)
LINQPad 似乎不仅执行其查询,而且还在其自己的 AppDomain 中加载其依赖项。如果您在应用程序中创建新的AppDomain,则可以以友好的方式处理异常并以友好的方式重新加载/编译应用程序。
更有趣的是 LINQPad 如何处理其依赖项。显然,当加载到这些 AppDomain 中时,它们是“影子复制”的,因为我有一个自己开发的自定义库,并且我能够“即时”对其进行更改,并且 LINQPad 不会锁定文件;相反,它似乎有一个 FileSystemWatcher,它会查找文件的更改,卸载 AppDomain,然后使用新的依赖项重新加载 AppDomain。
构建新库后应用程序中的“暂停”,然后我添加的那些新“方法”现在可通过智能感知供脚本使用,这表明 LINQPad 在处理脚本和引用的库时相当智能:a)不仅改变;但 b) 可能会导致其崩溃。
但是,如果您确实想享受一些乐趣,则始终可以使用 System.Diagnostics.Debugger.Break()。如果您在 LINQPad 属性中关闭脚本优化,您实际上可以调试到 LINQPad 进程,在 Visual Studio 调试器窗口中获取源代码,放置断点,单步执行,检查变量等,以便实际“调试” ' 在 LINQPad 中创建的代码片段。这会在您的脚本中增加几行,但如果您在 LINQPad 中进行一些繁重的脚本编写/测试,那么这是非常值得的。
这仍然是我能找到的用于原型设计、单元测试和构建 C# 代码的最佳工具之一,这些代码使用 LINQ 查询,我可以直接剪切和构建 C# 代码。粘贴到应用程序中时,感觉它们会像观察到的那样表现得相对容易。
LINQPad seems to not only execute its queries, but also load its dependencies within their own AppDomain. If you create a new AppDomain within your application, you can handle the exceptions in a friendly way and reload/compile the application in a friendly manner.
Even more interesting is how LINQPad handles its dependencies. Apparently they are "shadow-copied" when loaded into these AppDomains, since I have a custom library that I have developed and I am able to make changes to it "on-the-fly" and LINQPad doesn't lock the file; instead, it seems as if it has a FileSystemWatcher which looks for changes to the file, unloads the AppDomain, then re-loads the AppDomain with the new dependency.
The 'pause' in the application after building a new library and then those new 'methods' that I have added are now available to scripts through intellisense indicates that LINQPad is rather intelligent when dealing with scripts and referenced libraries that: a) not only change; but b) could cause it to crash.
However, you can always use the System.Diagnostics.Debugger.Break() if you really want to have some fun. If you turn off optimization on your scripts in the LINQPad properties, you can actually debug into the LINQPad process, get your source code in a Visual Studio debugger window, place breakpoints, step through, examine variables, etc. in order to actually 'debug' your code snippets created in LINQPad. It's a couple of extra lines in your script, but well worth it if you do some heavy scripting/testing within LINQPad.
This is still one of the best tools I have been able to find to prototype, unit test and build up C# code which uses LINQ queries that I can just cut & paste into applications with a relatively easy feeling they will behave as observed.