Is it guaranteed that a thread will complete in aspnet?

前端 未结 2 1553
滥情空心
滥情空心 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:22

    Unless the thread is aborted or the above statements throw exceptions, yes, it will always be created (leaving file permissions and IO problems out of the picture).

    Reasons the thread might be aborted:

    • (Manual) application pool recycle;
    • Request abandoned;
    • Crash of application.
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题