什么会导致 .NET 进程/线程意外终止?
我试图收集 .NET 进程或线程终止原因的完整列表,即使 main() 方法由 try...catch 子句保护。
其中一个原因是 Thread.Abort()(除非您调用 Thread.ResetAbort)。 你还知道更多的原因吗?
I'm trying to gather a complete listing of the reasons a .NET process or thread to terminate, even though the main() method is guarded by a try...catch clause.
One such reason is Thread.Abort() (unless you call Thread.ResetAbort). Do you know of more reasons?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的代码无法处理 StackOverflowException。
当您有无限循环时,通常会发生 StackOverflowException,这会让您的调用堆栈不断增长,直到超出通常的堆栈大小 (1MB)。
还有更多无法恢复的异常情况。 ExecutionEngineException 似乎是其中之一。
StackOverflowException cannot be handled by your code.
A StackOverflowException typically occurs when you have an endless loop which lets your call-stack grow until the usual stack size (1MB) is exceeded.
There are more exceptions where you cannot recover from. ExecutionEngineException seems to be one of them.
与 C/C++ 不同,main() 并不完全是应用程序的全部。 因此,即使使用 try/catch 块包围 main() 中的所有代码也不会捕获该代码生成的所有异常。
但是,您可以附加一个函数来通过侦听 Application.ThreadException 事件来处理整个应用程序引发的未处理的异常,这将帮助您捕获应用程序中任何线程的异常,无论该异常是否由您的代码创建。
例如,您的代码可能会调用外部非托管 DLL 中的代码。 该代码可能会执行自己的线程,这可能会异步失败,从而导致引发异常。 该异常属于您的应用程序进程,但不属于您的任何代码。 如果未捕获,将导致您的程序意外终止。
Unlike in C/C++, the main() is not quite the entirety of your application. So even surrounding all the code in main() with a try/catch block will not catch all exceptions generated by that code.
However, you can attach a function to handle unhandled exceptions thrown by the entire application by listening to the Application.ThreadException event, which will help you catch exceptions from any thread in the application, whether it was created by your code or not.
For example, your code may call upon the code in an external, unmanaged DLL. That code might execute threads of its own, which might asynchronously fail, causing an exception to be thrown. That exception belongs to the process that is your application, but not to any of your code. And if uncaught, will cause your program to terminate unexpectedly.
也许你的线程中发生了未处理的异常,最终导致线程被杀死; 在主线程中有一个 try catch 子句不会捕获另一个正在运行的线程的异常。
编辑:
共享字段读/写的一些并发访问
Maybe there is an unhandled exception occurring in your thread, ending in killing the thread; having a try catch clause in your main thread doesn't catch and exception from another thread running.
EDIT:
Some concurrent access in read/write of a shared field
网络连接超时。
停电。
用户终止了相关进程。
Network connection timing out.
Power outage.
A user killing the process in question.
卸载AppDomain
Unloading the AppDomain