Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does multithreading make sense in asp.net?

In winforms development you may create a BackgroundWorker to avoid locking the UI on a long running process. In ASP.NET a POST/GET basically freezes until completion. I don't see how adding a Thread would be of any benefit to this process. Maybe if the Thread would speed completion (say on a multi-core server) it could help speed things up.

In the general sense I could use AJAX to make long calls without causing a "freeze" of the web page. In this sense AJAX can be thought of as a substitute for threading.

Is that it? Is threading pretty much useless on ASP.NET?

like image 229
P.Brian.Mackey Avatar asked Jun 03 '11 19:06

P.Brian.Mackey


People also ask

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.

Can you do 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.

Is multithreading a good idea?

Multithreading also leads to minimization and more efficient use of computing resources. Application responsiveness is improved as requests from one thread do not block requests from other threads. Additionally, multithreading is less resource-intensive than running multiple processes at the same time.

Why multithreading is used in C#?

Multithreading is a feature provided by the operating system that enables your application to have more than one execution path at the same time. Multithreading is a feature provided by the operating system that enables your application to have more than one execution path at the same time.


2 Answers

Multithreading can make requests faster if:

  1. Each web request's work can be broken down into multiple tasks that can run in parallel
  2. Each of those tasks is very CPU intensive (CPU bound), or you have multiple I/O bound tasks on different I/O paths (see comments)
  3. You can implement them using fork/join parallelism.

In that case, you can indeed use multithreading in ASP.NET. A common mistake that people make is to spawn off threads and then not wait for them to complete- for instance "I am going to respond to the user while logging their purchase to the database on a background thread. This is always a bad idea, because IIS can and will recycle the Application Pool that your website is running in, and those background threads might be silently aborted.

like image 126
Chris Shain Avatar answered Sep 29 '22 22:09

Chris Shain


Threading can be useful in server side programming if you need to return a response and don't want to tie up the server thread, in particular for long running processes that would cause the request to time out.

like image 36
Oded Avatar answered Sep 29 '22 23:09

Oded