Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreading in asp.net

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

like image 933
Larry Foulkrod Avatar asked Sep 08 '08 17:09

Larry Foulkrod


People also ask

What is multithreading in .NET with example?

Multithreading in C# is a process in which multiple threads work simultaneously. It is a process to achieve multitasking. It saves time because multiple tasks are being executed at a time. To create multithreaded application in C#, we need to use System. Threding namespace.

Is ASP.NET multithreaded?

ASP.NET platform is multithreaded by its nature and provides programming models that shield us from the complexity of using threads.

Is there multithreading in C#?

Along with this, C# provides an option to execute the code that can be run in parallel using a multithreading concept, where the process/application can have multiple threads invoked within it.

What is multithreading and its types?

Multithreading is running multiple tasks within a process. It is of two types, namely user level threads and kernel level threads. It is economical, responsive, scalable, efficient, and allows resource sharing. There are three models in multithreading: Many to many model, Many to one model, and one to one model.


3 Answers

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.

like image 129
Eric Z Beard Avatar answered Nov 12 '22 14:11

Eric Z Beard


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.

like image 20
kemiller2002 Avatar answered Nov 12 '22 14:11

kemiller2002


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.

like image 2
Jason Baker Avatar answered Nov 12 '22 14:11

Jason Baker