且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在.NET通用线程池

更新时间:2023-11-13 18:45:46

这听起来像你所谈论的工作队列? (我听起来像大眼夹...)

It sounds like you are talking about a work queue? (and I sound like clippy...)

有关记录,线程池中的线程通常应该用于工作的小品。理论上,应该创建自己的线程长寿命队列。需要注意的是.NET 4.0,可以采用CCR / TPL库,所以我们会得到一些内在的工作队列*** - 但它并不难写螺纹工作队列。而且你可以把它通用的,太;-p

For the record, thread-pool threads should typically be used for short pieces of work. You should ideally create your own threads for a long-lived queue. Note that .NET 4.0 may be adopting the CCR/TPL libraries, so we'll get some inbuilt work queues for free - but it isn't hard to write a threaded work-queue. And you can make it generic, too ;-p

重的问题 - 我更喜欢捕捉变量的方法来传递状态进入线程(无论是螺纹线程池 Control.Invoke ):

Re the question - I prefer the captured variables approach to passing state into threads (be they Thread, ThreadPool, or Control.Invoke):

    Thread t = new Thread(() => SomeMethod(arg));
    t.IsBackground = true;
    t.Name = "Worker n";
    t.Start();

这使您线程更精细的控制,不会出现饱和线程池

This gives you much more granular control over the thread, without saturating the ThreadPool.