简体   繁体   English

线程池中引发异常

[英]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: 因此,让我们回顾一下您拥有的东西:

  • You have a method executing on a TheadPool thread. 您有一个在TheadPool线程上执行的方法。
  • This method will throw an exception. 此方法将引发异常。
  • The exception will not be handled by a try-catch block. try-catch块不会处理该异常。
  • The application is running outside of the IDE. 该应用程序正在IDE外部运行。

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外部运行时,您将看到以下内容。

  • A dialog box will appear saying "[application] has encountered a problem and needs to close. We are sorry for the inconvenience." 将出现一个对话框,指出“ [应用程序]遇到问题,需要关闭。给您带来的不便,我们深表歉意。”
  • The application will continue to run and print the current time each second. 该应用程序将继续运行并每秒打印当前时间。
  • The application will terminate after clicking Close on the dialog box. 单击对话框上的关闭 ,应用程序将终止。

When running inside the IDE 1 you will observe the following. 在IDE 1中运行时,您将看到以下内容。

  • The IDE will intercept the exception and display it using its own unhandled exception window. IDE将拦截该异常,并使用其自己的未处理异常窗口显示该异常。
  • All threads will be halted which means you will not see the current time being displayed. 所有线程将被暂停,这意味着您将看不到当前时间。
  • Ignoring the exception and resuming the debugging session will allow the current time to begin displaying. 忽略异常并恢复调试会话将允许当前时间开始显示。
  • The IDE will again rethrow the exception and intercept it. IDE将再次抛出该异常并对其进行拦截。
  • All threads will be halted and the cycle will repeat indefinitely. 所有线程将被暂停,并且循环将无限期重复。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM