views:

94

answers:

3

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: http://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...

+1  A: 

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

Mitchel Sellers
+3  A: 

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.

Achilles
@Achilles: I intend to do that too, but a) some reports are best generated as soon as is reasonably possible and b) if I can use idle time I could use this for a Geni.com-style "I'll tell you as soon as it's ready" type report generation.
Scott Stafford
I'm not convinced that you can do this...wanting XXXX.com-style reports is fine but without knowing their approach how can you acheive it when you have known limitations imposed upon you? And given "spiky" nature of cpu utilization in an application underload I don't think there will be large enough windows to not drag down the sites performance when creating the reports.
Achilles
@Achilles: exactly, that's why the "low priority thread" is critical.
Scott Stafford
@Achilles: An even more important reason against this is that IIS app pools often terminate themselves after being idle for a while, so they won't be around to wake themselves up!
Scott Stafford
A: 

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...

Scott Stafford