Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...