Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

right way to create thread in ASP.NET web application

i'm creating asmx web service and have to create thread to do background IO to refresh system data. What is the right way? I'm not interested to get any results to creating thread. I just want the ASP.NET worker thread to create a thread that does it's loading and in the end makes one assign (I think assign _alldata = newData is atomic where both instances of my own big structure class SystemData) so the worker thread that created the the new thread can propagate instantly.

I read an article http://msdn.microsoft.com/fi-fi/magazine/cc164128%28en-us%29.aspx#S2 which suggest to use non-threadpool thread. The article however was about different / more complex scenario and didn't help me so much.

Thanks: Matti

PS. I have asked this question also in what is the right way to spawn thread for database IO in asmx web service? but that was too complex with multiple questions.

like image 402
char m Avatar asked Dec 01 '09 09:12

char m


People also ask

Does ASP.NET use threads?

Every ASP.NET web application has its own pool of threads that is used for serving requests. Limitations of this pool can be configured on web server level as well as application level.

What is threading in ASP NET MVC?

A thread is used for every single hit, whether it's for an ASP.NET page or a static file there's a thread handling it on the server. (Not all of those threads will involve . NET threads and their particular overhead).

Is ASP.NET single threaded?

In short, ASP.NET is multithreaded and when processing a particular request, may even switch to another thread at particular steps during the request life cycle (and it could be worse with async pages).


2 Answers

Something like this:

public delegate void Worker(); private static Thread worker;  public static void Init(Worker work) {     worker = new Thread(new ThreadStart(work));     worker.Start(); }  public static void Work() {     // do stuff } 

Then get things started by calling Init(Work).

If you call BeginInvoke() or ThreadPool.QueueUserWorkItem(), it uses an ASP.NET thread pool thread, which can impact the scalability of your application.

In case it's useful, I cover these issues in detail in my book, along with code examples, sample benchmarks, etc: Ultra-Fast ASP.NET.

like image 70
RickNZ Avatar answered Oct 01 '22 03:10

RickNZ


Take a look at:

http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx?fid=326357&df=90&mpp=25&noise=3&sort=Position&view=Quick

You can do something like:

 public delegate void MethodInvoker();

    private void Foo()
    {
        // sleep for 10 seconds.
        Thread.Sleep(10000);
    }

protected void Button2_Click(object sender, EventArgs e)
{
    // create a delegate of MethodInvoker poiting to
    // our Foo function.
    MethodInvoker simpleDelegate = new MethodInvoker(Foo);

    // Calling Foo Async
   simpleDelegate.BeginInvoke(null, null);

}
like image 44
SirMoreno Avatar answered Oct 01 '22 02:10

SirMoreno