能够保证所有的线程将在ASPNET完成? [英] Is it guaranteed that a thread will complete in aspnet?

查看:85
本文介绍了能够保证所有的线程将在ASPNET完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASPNET这个简单的code:假定没有例外,也没有文件锁定,也不进程终止:

I have this simple code in aspnet :assuming no exceptions nor file locking nor process terminates :

    new Thread(()=>{
          Thread.sleep(15000);
         // GC.Collect(); 
          File.Write (...); // dummy file

    }).Start();

   // GC.Collect();

这是我的测试中,该文件的总是的创建。

From my tests , the file is always created.

结果
这是不理解我的情况是,请求寿命比线程执行短得多,仍然它的作品。就这样 。那是我的主要问题。

   
The scenario which is not understood to me is that the request lifetime is much shorter than thread execution and still it works . Thats all . Thats my main question  

时它总是保证文件将被创造出来的?

Is it always guaranteed that the file will be created ?

铌它只是一个测试,以检查行为。
另外,页面仅与该code空白页。此外,我知道,加入等待

Nb it's just a test to examine behavior . Also the page is an empty page with only this code. Also i know that join will wait

推荐答案

这是不。当IIS决定recyle的整个过程将花费下来后台和前台线程。请参见这个答案

It is not. When IIS decides to recyle the entire process it will take down both background and foreground threads. See this answer.

幸运的是,你的可以的确保他们不会被中断。您可以通过执行 <告诉ASP.NET关于你的线程code> IRegisteredObject 接口。然后,您可以使用注册对象<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.hosting.hostingenvironment.registerobject.aspx\"相对=nofollow> HostingEnvironment.RegisterObject 。见<一href=\"http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx/\"相对=nofollow>实施定期后台任务在ASP.NET 的危险

Luckily, you can make sure they are not interrupted. You can tell ASP.NET about your thread by implementing the IRegisteredObject interface. You can then register your object using HostingEnvironment.RegisterObject. See The Dangers of Implementing Recurring Background Tasks In ASP.NET

这篇关于能够保证所有的线程将在ASPNET完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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