Is it guaranteed that a thread will complete in aspnet?

前端 未结 2 1602
滥情空心
滥情空心 2021-01-24 07:50

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

    new Thread(()=>{
          Thread.sleep(15000);
             


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-24 08:32

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

    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

提交回复
热议问题