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

发布于 2024-09-16 01:03:32 字数 294 浏览 3 评论 0 原文

我在 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪花飘飘的天空 2024-09-23 01:03:33

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

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

  • Machine.Config、Web.Config 或 Global.asax 已修改
  • bin目录或其内容被修改
  • 重新编译次数(aspx、ascx 或 asax)超出限制
    由指定
    machine.config 中的设置或
    web.config(默认设置为
    • 修改虚拟目录的物理路径
    • CAS 政策已修改
    • 网络服务已重新启动
    • (仅限 2.0)应用程序子目录已删除

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

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

    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.

    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:

    • Machine.Config, Web.Config or Global.asax are modified
    • The bin directory or its contents is modified
    • 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
    • The physical path of the virtual directory is modified
    • The CAS policy is modified
    • The web service is restarted
    • (2.0 only) Application Sub-Directories are deleted

    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 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.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文