[英]Exception Thrown in thread pool
I have a thread pool and I use a library that if certain conditions are met to throw an exception. 我有一个线程池,并且我使用了一个库,如果满足某些条件,该库将引发异常。 My question is if this runs out of the IDE, will the thread come to an halt or will the whole entire program come to a halt?
我的问题是,如果这用尽了IDE,线程会停止还是整个程序都停止?
If the application is .Net 2+, an unhandled exception on a non-main thread will flatten the process. 如果应用程序是.Net 2+,则非主线程上的未处理异常将使进程变平。
You can configure this behavior using the app.config LegacyUnhandledExceptionPolicy
setting, but I wouldn't recommend this as it potentially masks serious bugs. 您可以使用app.config
LegacyUnhandledExceptionPolicy
设置配置此行为,但我不建议您这样做,因为它可能掩盖严重的错误。
Update 更新
If you wish to ignore the occasional timeout exception, consider placing your WebService call in a try\\catch block and ignore the timeout exception: 如果您希望忽略偶尔的超时异常,请考虑将WebService调用放在try \\ catch块中,并忽略超时异常:
try
{
//Call WebService
}
catch(System.Net.WebException ex)
{
//Ignore
}
So lets review what you have: 因此,让我们回顾一下您拥有的东西:
TheadPool
thread. TheadPool
线程上执行的方法。 try-catch
block. try-catch
块不会处理该异常。 Now we can easily test this with the following code. 现在,我们可以使用以下代码轻松进行测试。 Pay careful attention to the observations below, especially the bold one, because they are interesting in the context of your question.
请仔细注意以下观察,特别是大胆的观察,因为它们在您的问题中很有趣。
public static void Main()
{
ThreadPool.QueueUserWorkItem(
(state) =>
{
throw new InvalidOperationException();
}, null);
while (true)
{
Thread.Sleep(1000);
Console.WriteLine(DateTime.Now.ToString());
}
}
When running outside of the IDE you will observe the following. 在IDE外部运行时,您将看到以下内容。
When running inside the IDE 1 you will observe the following. 在IDE 1中运行时,您将看到以下内容。
1 I tested this with Visual Studio 2010. 1我使用Visual Studio 2010对此进行了测试。
When the debugger breaks a program, all threads come to a halt. 当调试器中断程序时,所有线程都将停止。
In the Debug -> Windows -> Threads
screen, you can see the threads. 在
Debug -> Windows -> Threads
屏幕中,您可以看到线程。 You can "freeze" a thread, which means it will stay halted if you continue running the program. 您可以“冻结”线程,这意味着如果继续运行该程序,它将保持暂停状态。
So it is possible to keep some threads halted even when the debugger runs. 因此,即使调试器运行,也可以使某些线程停止。 The opposite is not possible: no thread can run when the debugger is in break mode.
相反是不可能的:当调试器处于中断模式时,没有线程可以运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.