views:

308

answers:

2

Hello, 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 http://stackoverflow.com/questions/1820770/what-is-the-right-way-to-spawn-thread-for-database-io-in-asmx-web-service but that was too complex with multiple questions.

A: 

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);

}
SirMoreno
BeginInvoke() uses an ASP.NET thread pool thread.
RickNZ
thanks for the answer! now there seems to be debate with you and RickNZ whether to use thread pool thread or not...
matti
If the thread isn’t going to use a lot of CPU time (waiting for third party like DB) you better off using a custom thread pool: http://stackoverflow.com/questions/1031763/net-custom-threadpool-with-separate-instancesOtherwise I think BeginInvoke is the right way.
SirMoreno
A: 

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.

RickNZ
Isn’t starting a new thread takes more resources then using a thread from the thread pool ?
SirMoreno
thanks for the answer! now there seems to be debate with you and SirMoreno whether to use thread pool thread or not...
matti
Yes, starting a new thread uses more resources than a threadpool thread. However, the idea is not to start a new thread for every action. Instead, start just one (or a small number) of your own threads, and then pass WorkItems of some kind to them for execution. Again, the problem with threadpool threads is that they will reduce the scalability of your app, because they are the same threads that are used to process the pages themselves.
RickNZ
thanks again Rick. think i'll go with non-threadpool thread. this is done every 24h. So not for every action ;)
matti