长如何运行的内部Web应用程序的线程工作 [英] How can long running thread work inside web application

查看:176
本文介绍了长如何运行的内部Web应用程序的线程工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我MVC控制器方法里面以下code:

So I have inside MVC controller method the following code :

public ActionResult ProcessFile () 
{
    ThreadStart threadStart = new ThreadStart ( ()=>{

        // Doing some long processing that takes 20 minute 
    } );

    Thread thread = new Thread(threadStart); 
    thread.Start();
}

这里的问题是,当多个请求发送到该控制器方法的线程会被杀掉。
我需要主题留直到结束处理工作,并且它似乎它是多少资源获取所采取的少的线程可以住工作资源的问题。
如果我运行从Windows应用程序或服务的过程中,它完美的作品,它只是有问题,当它得到来自Web应用程序启动。

The problem here is that when more than one request sent to this controller method the thread get killed. I need to Thread to stay working untill the processing ends , and it seem it is a matter of resources the much resources get taken the less threads can stay working . If I run the process from windows application or service it works perfectly , it is only having problem when it get started from web application .

推荐答案

在理想情况下,你不应该在IIS 7中创建线程(长时间运行的后台作业)关闭Web请求。

Ideally you should not create threads (for long running background jobs) off web requests in IIS 7.

应的应用程​​序池/ AppDomain重新启动,这可能会发生的一些原因,如改变到bin文件夹的内容,修改web.config中,闲置一段时间,资源门槛的线程将被停止到达(如内存使用情况)。事实上在AppDomain将没有其他原因比过去活的时间X数量定期重新启动默认情况下。

The threads will be stopped should the app pool/AppDomain restart, which could happen for a number of reasons such as changes to the contents of the bin folder, changes to web.config, a period of inactivity, a resource threshold is reached (e.g. memory usage). In fact the AppDomain will restart periodically by default for no other reason than it has been alive for x amount of time.

由于这个原因长时间运行的任务应该使用作业队列和后​​台作业亚军,例如实施控制台应用程序或Windows服务(重要的,而不是在IIS的上下文中运行),以处理这些工作。

For this reason long running tasks should be implemented using a job queue and background job runner, e.g. console app or windows service (importantly, not running in the context of IIS), processing those jobs.

该网站的请求应该工作简单地添加到队列,允许他们出现在后台作业亚军拿起作业。

The web request should simply add the job to the queue allowing the background job runner to pick up the jobs as they appear.

有一个类似的问题已经在这里<一回答href=\"http://stackoverflow.com/questions/536681/can-i-use-threads-to-carry-out-long-running-jobs-on-iis\">Can我使用线程来进行IIS上长期运行的工作?

A similar question has been answered here Can I use threads to carry out long-running jobs on IIS?

编辑:A 认真的不建议使用替代

A seriously not recommended alternative

如果你坚持还是别无选择,只能运行在IIS进程在后台工作,你需要看看允许后台作业是自发停止,并允许它从它离开的地方,当它被停止重启。

If you insist or have no alternative but to run the background job in the IIS process, you need to look at allowing the background job to be spontaneously stopped, and allowing it to restart from where it left off when it was stopped.

您可以有哪些可以轮询来触发未完成作业的重新启动的URL。

You can have a URL which can be polled to trigger the restart of unfinished jobs.

我已经看到在过去这项工作,实现将根据后台工作需要什么样的复杂程度而定。

I have seen this work in the past, the implementation will vary in complexity based on what the background job entails.

您会发现,服务器可能会开始有性能问题,因为后台作业启动,并多次被杀害。

You will find the server may begin to have performance issues as background jobs are started and killed repeatedly.

这篇关于长如何运行的内部Web应用程序的线程工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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