Ilya Smagin Ilya Smagin - 1 month ago 17
ASP.NET (C#) Question

ASP.NET MVC Multithreading

I want to implement such logic in my asp-net-mvc application:

user clicks a button
->

server executes some time-consuming logic in ~15 threads (i get data from really slow independent sources)
->

when all work is done, server merges the results and passes it back to user


The other day i've seen an article which explained why creating new Threads in asp-net application is highly not recommended, and ThreadPool is the one that should be used.
What are best practices for mvc in this case? Why shouldnt i create my threads, backgroundworkers, tasks, whatever by myself and use threadpool? The Application will be hosted on a public server, if it matters.

Answer

If you're using .Net 4, I would even recommend looking at the parallel namespaces. They make this even simpler and do a better job of utilizing all your CPU cores.

I would also look at offloading this from your main web app altogether. Having a separate set of services or a message queue to handle this long running request will let you scale a lot more easily and will allow your web app to worry about servicing page requests, not performing long running logic. Google up something like "iis long running request" to get started.

Comments