views:

291

answers:

3

I have a scheduled task that I need to kick off by browsing to a page, my webhost doesn't allow me to use the windows scheduler.

One job this task performs is to get some data from a bunch of other websites, due to how slow web-requests can be I've set up the task to use threading.

Running this task from an mvc action seems to cause the whole webserver to go wacky, making it refuse to load any more pages.

Is it bad to use threading from a web-app? Is there a way for me to safely do it? What threading limits are there? I really just need to know some more information?

EDIT: SUBQUESTIONS

If I use threads from the threadpool (which is used by the ASP.NET runtime (thanks Anton), can I limit how many of the threads it can use? What is the ASP.NET threadpool size limit?

Would be be better to have a separate site which does this? Perhaps a virtual folder containing a separate ASP.NET application? That would stop me from limiting the threadpool right?

A: 

I think it depends on how it is done. I am not terribly competent when it comes to threading under ASP.NET, but I think that there is a risk that you end up using threads that would otherwise be used to serve requests from clients.

Here is a tutorial on multithreading in ASP.NET, that might help you forward a bit.

Fredrik Mörk
A: 

I would be surprised if it actively caused a problem, as long as those threads don't try to talk back to the request/response (which may now be gone). What symptoms are you seeing? Can you define "wacky"?

I use threads routinely for logging etc, without issue.

To minimise the impact, you could use a producer/consume pattern, so you only have one (or a few) worker threads, servicing however-many ASP.NET MVC request/response threads.

Marc Gravell
By 'Wacky' I mean that it finishes processing my task (which should have ended all my spawned threads, as I wait for them to end / timeout), and returns View(), but the browser never finishes receiving the page, and any subsequent request for any other page of the site just hangs.
Matthew Rathbone
+2  A: 

When servicing incoming requests, ASP.NET runtime acquires threads from .NET ThreadPool, which has limited capacity. So if you're using threads from that tread pool, this may harm your applications' scalability.

The way around this will be spawning a usual Thread, but these can be killed when your AppPool gets recycled.

Anton Gogolev
I think you just solved my issue. Because I was running this from the command line I was changing the threadpool size. This seems to be the thing causing it to fall over. Sub question then: can I limit how many threads from the threadpool it uses?
Matthew Rathbone
@Matthew What exactly are you running from the command line?
Anton Gogolev
Sorry, that was badly written. My task used to be encapsulated in a console app for me to manually execute on-demand. It ran without issue, and only started causing problems when I executed it from the website. You spurred my memory when you mentioned threadpool size.
Matthew Rathbone
Well, if it's a console app, it is executed in a separate process. Since ThreadPools are per-process, this app should not theoretically affect ThreadPool of your web site. The culprit is probably elsewhere.
Anton Gogolev
Yup, thats why it hasn't been an issue up to this point. These problems began when I moved the process from a console app to a MVC controller action.
Matthew Rathbone