1

We have an asp.net application on an iis 7. we need to create an async process to do calculating (using a web service) and not keeping the client waiting, we used ThreadPool to do that (i prefer Tasks but i am a server side guy).
My questions are :
1. if using a thread pool on iis does it take threads from the iis pool from clients or from the OS ?
2. What would you use Tasks or ThreadPool (Tasks give you much more i know but the UI guys like the pool).
Tanks

guyl
  • 2,158
  • 4
  • 32
  • 58
  • If you're worried about fully loading your server, I would consider using a separate server to process the async requests so that you can scale each type of usage separately (applies if you expect your volume to necessitate more than a handful of servers in total). – Eric J. Jul 14 '11 at 17:42
  • we are using the calculating on a different server, but the call to the server and the returned answer is still on the same one – guyl Jul 14 '11 at 18:13

2 Answers2

4

The ASP.NET host provides a single thread pool per process. So if you're using ThreadPool, then it's taking from the same pool that is drawn from for server requests.

That said, if you're starting background operations that are independent from a single client request, then you should be using a Win32 service, web service, message queue system, or something similar. Running "background" threads in ASP.NET goes against the entire architecture of IIS; it's much easier to scale properly and do other IT work (e.g., restarting app pools) if you maintain the stateless nature of HTTP.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Just to make clear because i didn't quite understood, your recommendation is to create some other independent service (Some process separated from the current client request ) and transfer it to it? is it possible to create on the same app different stat thread and use the consumer producer design ? – guyl Jul 14 '11 at 18:07
  • The key is what you mean by "not keeping the client waiting". If you mean that a page request is used to start an operation and the page request completes before the operation is complete, then yes, it should be a different service (not hosted by ASP.NET). – Stephen Cleary Jul 14 '11 at 19:04
  • If task pulls a thread from the IIS thread pool, will that prevent a recycle from stopping your task? For example, say I want to delete a file on a remote SAN, but I don't want the client to wait for the delete. I'd like to just call Task.Run(IO.File.Delete(....)). Is this bad? If so, my server has oodles of RAM and cores. Can't I just configure more threads to be created in the pool? – Brain2000 Oct 26 '16 at 04:22
  • @Brain2000: No, it won't prevent a recycle. If a recycle occurs, the work you're doing on a thread pool thread will be aborted. I describe alternatives [here](http://blog.stephencleary.com/2014/06/fire-and-forget-on-asp-net.html). RAM/cores are immaterial; ASP.NET *must* recycle periodically because of the (IMO idiodic) way they handle connection aborts. – Stephen Cleary Oct 26 '16 at 13:10
0

Task, still consumes threads from thread pool.

As far, as I understood you question, 3rd party service performs CPU-heavy calculation, and you need asynchronously wait it to complete.

In case, if you want to postpone page rendering, until computation is complete, Async pages will help. This approach will return processing thread to thread-pool, while waiting.

Could you provide more details about required solution, for more precise answer?

Valera Kolupaev
  • 2,285
  • 14
  • 14
  • We are uploading a file from the client to our server and manipulating it. this action takes up to a minute in large files. this is preformed on different server. when calling the proxy client we want to use it on different thread in order not keeping the client to wait. by using threadPool we are taking a thread waiting a call from another client no ? – guyl Jul 14 '11 at 19:59
  • 1
    As Sthepen said, waiting for a web service to complete in as separate Task/ThreadPool_WorkItem will occupy one of IIS processing threads. In this case, you will not be able to serve, more that 25 clients at the same time. If you need wait for completion of WebService do in via Async pattern, by generating async proxies for WS and Async page rendering. However, if you want to show nice progress bar for the user, it's a different story. – Valera Kolupaev Jul 14 '11 at 20:17
  • Isn't it better to use a thread from the IIS thread pool? My thinking is if you are using your own thread that a recycle might kill a non-pool thread because it is unaware of its existence. – Brain2000 Oct 26 '16 at 02:05