为什么工作线程使我的ASP.NET生成器在睡眠期间遇到ThreadAbortException?

时间:2022-04-07 00:16:58

I spawn a worker thread in an ASP.NET application running on Windows Server 2008, IIS 7.5 The first thing this worker thread does is sleep for N seconds, and then it does its real work. During the sleep, a catch a ThreadAbortException.

我在Windows Server 2008,IIS 7.5上运行的ASP.NET应用程序中生成一个工作线程。这个工作线程做的第一件事就是睡眠N秒,然后它才能真正起作用。在睡眠期间,捕获一个ThreadAbortException。

Can you explain this behavior, and bonus points of you point me to any IIS/ASP.NET settings that can be used to adjust the behavior.

你能解释一下这种行为,并且你的奖励点指向我可用于调整行为的任何IIS / ASP.NET设置。

EDIT: More info. The suggestion to catch the ThreadAbortException helped me work around the problem, so thanks. I totally rewrote the wording of this question based on what I learned, but still, the same question, WHY is this worker thread getting aborted during a sleep?

编辑:更多信息。捕获ThreadAbortException的建议帮助我解决了这个问题,谢谢。根据我学到的东西,我完全重写了这个问题的措辞,但同样的问题,为什么这个工作线程在睡眠中会中止?

1 个解决方案

#1


6  

A ThreadAbortException happens on your worker thread because someone else called Thread.Abort on it, so it's probably nothing directly that your worker thread did, but rather some external cause. The first place you should check is your own code for any thread management or aborting that you might do. Otherwise, for IIS, this could be due to a worker process (w3wp.exe) or app pool, or AppDomain being recycled.

ThreadAbortException发生在你的工作线程上,因为有人在其上调用了Thread.Abort,所以它可能不是你的工作线程直接做的,而是一些外部原因。您应该检查的第一个地方是您自己的代码,您可以执行任何线程管理或中止。否则,对于IIS,这可能是由于工作进程(w3wp.exe)或应用程序池或AppDomain被回收。

The recycling might be due to the idle timeout setting for the app pool, a regularly scheduled recycle, or a memory/CPU use trigger. These are configurable through the IIS Configuration Manager in the Server Explorer (on Win 2K8) or by just running inetmgr.exe. According to Tess' blog here, there are a number of other reasons for AppDomain recycling:

回收可能是由于应用程序池的空闲超时设置,定期计划的回收或内存/ CPU使用触发器。这些可通过服务器资源管理器中的IIS配置管理器(在Win 2K8上)或仅运行inetmgr.exe进行配置。根据Tess的博客,AppDomain回收还有很多其他原因:

  • Machine.Config, Web.Config or Global.asax are modified
  • 修改了Machine.Config,Web.Config或Global.asax

  • The bin directory or its contents is modified
  • bin目录或其内容已修改

  • The number of re-compilations (aspx, ascx or asax) exceeds the limit specified by the setting in machine.config or web.config (by default this is set to 15)
  • 重新编译的次数(aspx,ascx或asax)超出了machine.config或web.config中设置指定的限制(默认情况下,此值设置为15)

  • The physical path of the virtual directory is modified
  • 修改虚拟目录的物理路径

  • The CAS policy is modified
  • CAS策略已修改

  • The web service is restarted
  • Web服务重新启动

  • (2.0 only) Application Sub-Directories are deleted
  • (仅限2.0)删除应用程序子目录

That blog post also has information on tracking down why a recycle happened. For starters, try looking in the Event Log (eventvwr.msc) to see if there's any detailed info.

该博客文章还提供了有关追踪回收发生原因的信息。对于初学者,尝试查看事件日志(eventvwr.msc)以查看是否有任何详细信息。

You could also try debugging the worker process directly. Attach the VS debugger to the w3wp.exe instance where your code runs, add a breakpoint on Thread.Abort (you might need to enable ".NET Framework source stepping" in the debugger options), and see where the Abort is originating from by using the call stack window. This won't tell you why it's happening, but at least you'll know who's doing it.

您也可以尝试直接调试工作进程。将VS调试器附加到运行代码的w3wp.exe实例,在Thread.Abort上添加断点(您可能需要在调试器选项中启用“.NET Framework源步进”),并查看Abort源自何处使用调用堆栈窗口。这不会告诉你它为什么会发生,但至少你会知道是谁在做这件事。

#1


6  

A ThreadAbortException happens on your worker thread because someone else called Thread.Abort on it, so it's probably nothing directly that your worker thread did, but rather some external cause. The first place you should check is your own code for any thread management or aborting that you might do. Otherwise, for IIS, this could be due to a worker process (w3wp.exe) or app pool, or AppDomain being recycled.

ThreadAbortException发生在你的工作线程上,因为有人在其上调用了Thread.Abort,所以它可能不是你的工作线程直接做的,而是一些外部原因。您应该检查的第一个地方是您自己的代码,您可以执行任何线程管理或中止。否则,对于IIS,这可能是由于工作进程(w3wp.exe)或应用程序池或AppDomain被回收。

The recycling might be due to the idle timeout setting for the app pool, a regularly scheduled recycle, or a memory/CPU use trigger. These are configurable through the IIS Configuration Manager in the Server Explorer (on Win 2K8) or by just running inetmgr.exe. According to Tess' blog here, there are a number of other reasons for AppDomain recycling:

回收可能是由于应用程序池的空闲超时设置,定期计划的回收或内存/ CPU使用触发器。这些可通过服务器资源管理器中的IIS配置管理器(在Win 2K8上)或仅运行inetmgr.exe进行配置。根据Tess的博客,AppDomain回收还有很多其他原因:

  • Machine.Config, Web.Config or Global.asax are modified
  • 修改了Machine.Config,Web.Config或Global.asax

  • The bin directory or its contents is modified
  • bin目录或其内容已修改

  • The number of re-compilations (aspx, ascx or asax) exceeds the limit specified by the setting in machine.config or web.config (by default this is set to 15)
  • 重新编译的次数(aspx,ascx或asax)超出了machine.config或web.config中设置指定的限制(默认情况下,此值设置为15)

  • The physical path of the virtual directory is modified
  • 修改虚拟目录的物理路径

  • The CAS policy is modified
  • CAS策略已修改

  • The web service is restarted
  • Web服务重新启动

  • (2.0 only) Application Sub-Directories are deleted
  • (仅限2.0)删除应用程序子目录

That blog post also has information on tracking down why a recycle happened. For starters, try looking in the Event Log (eventvwr.msc) to see if there's any detailed info.

该博客文章还提供了有关追踪回收发生原因的信息。对于初学者,尝试查看事件日志(eventvwr.msc)以查看是否有任何详细信息。

You could also try debugging the worker process directly. Attach the VS debugger to the w3wp.exe instance where your code runs, add a breakpoint on Thread.Abort (you might need to enable ".NET Framework source stepping" in the debugger options), and see where the Abort is originating from by using the call stack window. This won't tell you why it's happening, but at least you'll know who's doing it.

您也可以尝试直接调试工作进程。将VS调试器附加到运行代码的w3wp.exe实例,在Thread.Abort上添加断点(您可能需要在调试器选项中启用“.NET Framework源步进”),并查看Abort源自何处使用调用堆栈窗口。这不会告诉你它为什么会发生,但至少你会知道是谁在做这件事。