我在 Windows Server 2008、IIS 7.5 上运行的 ASP.NET 应用程序中生成一个工作线程。该工作线程所做的第一件事是休眠 N 秒,然后开始真正的工作。在睡眠期间,捕获 ThreadAbortException。
你能解释一下这种行为吗?你的加分点是给我指出任何可用于调整该行为的 IIS/ASP.NET 设置。
编辑:更多信息。捕获 ThreadAbortException 的建议帮助我解决了这个问题,所以谢谢。我根据我所学到的知识完全重写了这个问题的措辞,但仍然是同样的问题,为什么这个工作线程在睡眠期间被中止?
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.
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.
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?
发布评论
评论(1)
您的工作线程上发生 ThreadAbortException ,因为其他人调用了
Thread.Abort
,所以它可能不是您的工作线程直接执行的操作,而是一些外部原因。您应该检查的第一个地方是您自己的代码,以进行任何线程管理或中止操作。否则,对于 IIS,这可能是由于工作进程 (w3wp.exe) 或应用程序池或 AppDomain 被回收所致。回收可能是由于应用程序池的空闲超时设置、定期计划的回收或内存/CPU 使用触发器造成的。这些可以通过服务器资源管理器(在 Win 2K8 上)中的 IIS 配置管理器或仅运行 inetmgr.exe 进行配置。根据苔丝的博客 此处,AppDomain 回收还有许多其他原因:
该博客文章还包含有关追踪原因的信息对于初学者,请尝试查看事件日志 (eventvwr.msc) 以查看是否有任何详细信息。
您也可以尝试直接将 VS 调试器附加到代码运行的 w3wp.exe 实例。在 Thread.Abort 上添加断点(您可能需要在调试器选项中启用“.NET Framework 源代码步进”),然后使用以下命令查看
Abort
的来源:调用堆栈窗口不会告诉您为什么会发生这种情况,但至少您会知道是谁在做这件事。A
ThreadAbortException
happens on your worker thread because someone else calledThread.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.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:
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.
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 theAbort
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.