Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it advisable to create thread for each client request?

Code:

class Controller
{
    Some Action Method()
    {
        ...
        ...
        new Thread(() =>
        {
            //WCF cal- wil execute for around 10 secs.
            var result = service.SubmitAndCreateApp(params);
            sessionModel.IsAppCreated = result; 
        }).Start();

        return jsonresult;
        }
}

Since my WCF call is taking too much time, I don't want to use thread pool and make it starve.

It is evident here that the thread is being created for each client request. How can I optimize this or any other alternative way to achieve this in .Net 4.0 (VS 2010)?

like image 790
Al. Avatar asked Dec 26 '22 19:12

Al.


2 Answers

To put it simply: no, don't do this.

That said, you can look at the Task Parallel Library (TPL) in ASP.Net, which can achieve exactly what you are trying to do.

Quick search yielded this posting, which I only glanced over but seems on-point:

http://vizagtechie.blogspot.com/2013/03/parallel-programming-in-aspnet-mvc.html

like image 112
Phil Sandler Avatar answered Dec 28 '22 10:12

Phil Sandler


No. Your server will get DDOS'ed completely. At the very least, request a thread from the thread pool rather than creating your own by hand. If the thread pool runs out, you'll be waiting for one to become available. The rest of the server will continue to be able to work. Of course, your mileage may vary based on many factors.

like image 22
Jesse C. Slicer Avatar answered Dec 28 '22 10:12

Jesse C. Slicer