9

What kind of multi-threading issues do you have to be careful for in asp.net?

1
  • The definitive blog on issues (albeit dated) is Phil Haacks haacked.com/archive/2011/10/16/… There are many frameworks for running background processes such as Quartz.NET and HangFire. If you can live with a 90 second constraint, you can even use QueueBackgroundWorkItem. If you're on Azure, Web Jobs or cloud services. Commented Aug 20, 2014 at 0:06

6 Answers 6

9

It's risky to spawn threads from the code-behind of an ASP.NET page, because the worker process will get recycled occasionally and your thread will die.

If you need to kick off long-running processes as a result of user actions on web pages, your best bet is to drop a message off in MSMQ and have a separate background service monitoring the queue. The service could take as long as it wants to accomplish the task, and the web page would be finished with its work almost immediately. You could accomplish the same thing with an asynch call to a web method, but don't rely on getting the response when the web method is finished working. From code-behind, it needs to be a quick fire-and-forget.

Sign up to request clarification or add additional context in comments.

2 Comments

You've seem to be advocating this technique in multiple question on StackOverflow, and I think that it's the way to go for long running process like for example, billing process. Is there any sample code that you can share? or a keyword to binged?
There are many frameworks for running background processes which are much easier to use than implementing all this with MSMQ. For example, •Quartz.NET and HangFire. If you can live with a 90 second constraint, you can even use QueueBackgroundWorkItem. If you're on Azure, Web Jobs or cloud services.
6

One thing to watch out for at things that expire (I think httpContext does), if you are using it for operations that are "fire and forget" remember that all of a sudden if the asp.net cleanup code runs before your operation is done, you won't be able to access certain information.

Comments

2

If this is for a web service, you should definitely consider thread pooling. Too many threads will bring your application to a grinding halt because they will eventually start competing for CPU time.

Is this for file or network IO? If so, you should also consider using asynchronous IO. It can be a bit more of a pain to program, but you don't have to worry about spawning off too many threads at once.

Comments

0

Programmatic Caching is one area which immediately comes to my mind. It is a great feature which needs to be used carefully. Since it is shared across requests, you have to put locks around it before updating it.

Another place I would check is any code accessing filesystem like writing to log files. If one request has a read-write lock on a file, other concurrent requests will error out if not handled properly.

Comments

0

Isn't there a Limit of 25 Total Threads in the IIS Configuration? At least in IIS 6 i believe. If you exceed that limit, interesting things (read: loooooooong response times) may happen.

Comments

0

Depending on what you need, as far as multi threading is concerned, have you thought of spawning requests from the client. It's safe to spawn requests using AJAX, and then act on the results in a callback. Or use a service as a backgrounding mechanism, which runs every X minutes and processes in the background that way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.