简体   繁体   English

在现有的IProducerConsumerCollection(T)上使用BlockingCollection(T)

[英]Using BlockingCollection(T) on existing IProducerConsumerCollection(T)

I'm exposing an IProducerConsumerCollection(T) from an interface that will periodically add items for another thread to consume. 我正在从接口中暴露IProducerConsumerCollection(T) ,该接口将定期添加项目以供另一个线程使用。

public interface IProducer<T>
{
    IProducerConsumerCollection<T> ProducerCollection { get; }
}

I was trying to use a BlockingCollection(T) with the existing collection, but it appears that adding to the IProducerConsumerCollection(T) directly is not supported: 我试图将BlockingCollection(T)与现有集合一起使用,但是似乎不支持直接添加到IProducerConsumerCollection(T)

var queue = new ConcurrentQueue<string>();
var blockingCollection = new BlockingCollection<string>(queue);

var task1 = Task.Run(() => {
    Console.WriteLine("Dequeued " + blockingCollection.Take());
    Console.WriteLine("Dequeued " + blockingCollection.Take());
});

var task2 = Task.Run(() => {
    Console.WriteLine("Enqueueing Hello");
    queue.Enqueue("Hello");

    Console.WriteLine("Enqueueing World");
    queue.Enqueue("World");
});

Task.WaitAll(task1, task2);

This will hang indefinitely as the BlockingCollection(T) will not notice the new items. 这将无限期地挂起,因为BlockingCollection(T)将不会注意到新项。

Is there similar functionality to the BlockingCollection(T).Take method or is there anything simpler than: 是否具有与BlockingCollection(T).Take方法类似的功能,或者有什么比以下方法更简单的方法:

static async Task<T> TakeAsync<T>(
    IProducerConsumerCollection<T> collection,
    CancellationToken token
)
{
    T result;

    while(!collection.TryTake(out result))
    {
        await Task.Yield();
        token.ThrowIfCancellationRequested();
    }

    return result;
}

This will hang indefinitely as the BlockingCollection(T) will not notice the new items. 这将无限期地挂起,因为BlockingCollection(T)将不会注意到新项。

Indeed - because you're adding directly to queue . 确实如此-因为您是直接添加到queue From the documentation: 从文档中:

Do not modify the underlying collection directly. 不要直接修改基础集合。 Use the BlockingCollection<T> methods to add or remove elements. 使用BlockingCollection<T>方法添加或删除元素。 The BlockingCollection<T> object can become corrupted if you change the underlying collection directly. 如果直接更改基础集合,则BlockingCollection<T>对象可能会损坏。

So your calls to queue.Enqueue should use blockingCollection instead. 因此,您对queue.Enqueue的调用应改用blockingCollection

In terms of your TaskAsync - you can use TryTake with a timeout of Infinite (-1) instead... but you might want to read Stephen Cleary's blog post on a similar topic , too... TaskAsync方面,您可以使用TryTake并使用Infinite (-1)超时...但是您可能也想阅读Stephen Cleary关于类似主题的博客文章 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM