Scott Stafford
Scott Stafford

Reputation: 44776

How do I exploit idle CPU time in an IIS App Pool?

I have an IIS 6.0-based C#/ASP.NET web site with a SQL server backend. I want to generate some computationally expensive reports (summaries, search engine indexes, etc...) in idle CPU time. I need the reports to be generated from WITHIN the IIS App Pool so it knows the proper configuration settings and (harder to fix) avoids the nightmarish security restrictions I've been placed under.

Can I start threads inside the AppPool's process that won't tie up the CPU, so it can continue serving requests unfettered? If so, how? What code and libraries?

I imagine it involves ThreadPool and thread priorities, but I couldn't find good coverage of low-pri threads and their interaction with the IIS web server and App pool.

[EDIT] http://msdn.microsoft.com/en-us/magazine/cc163854.aspx#S7 discusses using a Timer for this but doesn't directly state that the .NET framework will insure that the Timer thread is low-priority. This might be a solution, but is that assured?

[EDIT] This guy talks about important exception-related issues: http://flimflan.com/blog/SafelyRunningBackgroundThreadsInASPNET20.aspx

[EDIT] Interestingly, Stack Overflow itself seems to use IIS background threads for my purpose: https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/... in the comments, everyone says their (no longer used) technique sucks, but this one in the comments makes sense to me...

Upvotes: 4

Views: 694

Answers (3)

Scott Stafford
Scott Stafford

Reputation: 44776

As best I can tell, amazingly, there is no good answer. The best answer I can come up with is to run background threads in IIS, and have a separate polling requestor occasionally pinging the server to make sure it's awake. If anyone comes up with a better answer than this, I'll reassign the checkmark though...

Upvotes: 0

Achilles
Achilles

Reputation: 11299

Instead of polling the app pool for low cpu usage, why not ASSUME that the usage would be low when the system is under it's lightest load(after business hours etc...) Then you can simply schedule your reporting during that window and not worry about CPU usage straining the system.

Upvotes: 3

Mitchel Sellers
Mitchel Sellers

Reputation: 63126

You could start a thread, and run it in the background. That will give it a lower priority than regular UI threads.

Upvotes: 2

Related Questions