ASP.NET长时间运行的任务。线程被中止异常 [英] ASP.NET long running task. Thread is being aborted exception

查看:109
本文介绍了ASP.NET长时间运行的任务。线程被中止异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是ASP.NET 3.5 Web应用程序已启动多个任务需要时间来完成。由于显而易见的原因,这将启动这些任务的页面不能等待他们完成也不会有人要等待相当长的时间的响应,因此这些任务必须是异步的。

An ASP.NET 3.5 webapp has to start several tasks that takes hours to complete. For obvious reasons the pages which starts these tasks cannot wait for them to finish nor will anyone want to wait that long to get a response, so the tasks must be asynchronous.

有一个助手类来处理所有这些长时间运行的任务。该时间表和执行这些任务的主要方法是目前以下内容:

There is a Helper class to handle all of these long running tasks. The main method that schedules and executes these tasks is currently the following:

public static bool ScheduleTask(TaskDescriptor task, Action action)
{
    bool notAlreadyRunning = TasksAsync.TryAdd(task);
    if (notAlreadyRunning)
    {
        Thread worker = null;
        worker = new Thread(() => 
        {
            try { action(); }
            catch(Exception e)
            {
                Log.LogException(e, "Worker");
            }
            TasksAsync.RemoveTask(task);
            workers.Remove(worker);
        });
        workers.Add(worker);
        worker.Start();
    }
    return notAlreadyRunning;
}

在早期的实现我们使用了 ThreadPool.QueueUserWorkItem 的办法,但结果始终是相同的:后aprox的。 20-30分钟一个线程已被中止异常。

On earlier implementations we've used the ThreadPool.QueueUserWorkItem approach but the result has always been the same: after aprox. 20-30 mins a Thread was being aborted exception is thrown.

有谁知道为什么会出现这种情况?或者怎么能prevented?

Does anyone know why is this happening? or how can it be prevented?

更多信息:


  • IIS的标准配置。

  • 任务可以是任何东西,querys到数据库和/或IO操作等。

更新:决定

感谢大家的响应。现在我不知道该标记为回答这些问题。所有的人都有效,并且对这个问题的可能解决方案。将等待今天和标记为回答最选票答案,在一场平局,我会选择第一个显示答案的情况下,他们通常被大多数的相关性排序。

Thank you all for your responses. Now I don´t know which question to mark as answer. All of them are valid and are possible solutions to this problem. Will wait for today and mark as answer the answer with the most up votes, in case of a draw I will choose the first shown answer, typically they are ordered by most relevance.

对于任何人谁want's知道我选择,又由于时间限制的解决方案,是要更改IIS配置的回收,但我认为是理想的解决方案,根据我的研究,当然下面的答案,是创建工人服务,并使用ASP.NET应用程序和新的工人服务之间的通信解决方案,以协调做了长时间运行的工作。

For anyone who want´s to know the solution I choose, again due to time restrictions, was to change the IIS recycling configuration, But what I consider to be the ideal solution, based on my research and of course the answers below, is to create a "Worker Service" and use a communication solution between the ASP.NET App and the new "Worker Service" to coordinate the long running work to be done.

推荐答案

您可以开始在自己的应用程序域的长时间运行的进程。

You can start the long-running process in its own application domain.

在过去,当我需要这种能力,我创建一个Windows服务用于这一目的。如果你使用WCF连接到它,它甚至没有到IIS机器上运行在所有;你可以在网络上的任何一台机器上运行它。

In the past, when I've needed this capability, I create a Windows Service for this purpose. If you use WCF to connect to it, it doesn't even have to run on the IIS machine at all; you can run it on any machine on the network.

这篇关于ASP.NET长时间运行的任务。线程被中止异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆