OOD Waterball
OOD Waterball

Reputation: 801

How to make a thread working in the background upon IIS server even without any request to the web app?

I have a ASP.NET MVC web application deployed on IIS server,

In that app, I do a thing such like below in a independent thread:

while (true)
{
    doSomeJobs();
    Thread.Sleep(TimeSpan.FromMinutes(5));
}

Start this thread in Application_Start() so seems it will never stop only sleep for 5 minutes between every round of job.

First looks work well but in the next day I check the log it's not working as I expect, it's been stopped working for a very long time.

Then when I send a request again to the app, it begins to work again.

So what did I miss up? Is it possible to make a thread working in the background in every specific time even without any request to the app? Such like a scheduled task ?

Upvotes: 4

Views: 1512

Answers (1)

OOD Waterball
OOD Waterball

Reputation: 801

Finally I found this a common issue,

That's because IIS will stop your process in the specific time if nobody requests to your app.

To keep your app alive.

  1. Create a new application pool which needs to host always-alive app.
  2. Choose that application pool and click advanced setting.

enter image description here

  1. under recycling set Regular time interval to 0

enter image description here

  1. under process model set idle timeout (minute) to the max (43200)

enter image description here

Upvotes: 2

Related Questions