我正在寻找一种已经在c#中使用的机制,它允许我这样做:
任何关于使用c#的建议都能实现这样的目的吗?
发布于 2013-03-18 15:05:13
BlockingCollection<T>类使生产者/消费者队列非常容易使用。
var queue = new BlockingCollection<Action>();
//put work to do in the queue
for (int i = 0; i < 10; i++)
queue.Add(() => ProcessImage());
queue.CompleteAdding();
//create two workers
for (int i = 0; i < 2; i++)
{
Task.Factory.StartNew(() =>
{
foreach (var action in queue.GetConsumingEnumerable())
action();
});
}
//to cancel the work, empty the queue
Task.Delay(5000)
.ContinueWith(t =>
{
queue.GetConsumingEnumerable().Count();
});发布于 2013-03-18 14:52:13
使用Seamphore + ConcurrentQueue组合体
https://stackoverflow.com/questions/15479744
复制相似问题