I'm using a Queue<T>
for caching video. The idea is to fill it with开发者_运维问答 data (Enqueue
), start playing (Dequeue
) and fill back continously as data arrives. Can I do the filling back part from a background thread?
It sounds like you're looking for a producer/consumer queue. You can do this using Queue<T>
, but you'll need to add locking to make sure you never access the queue from multiple threads concurrently.
If you're using .NET 4, Parallel Extensions makes this much easier with IProducerConsumerCollection<T>
and BlockingCollection<T>
which do all the hard work for you.
Of course, you can, if you lock access to the queue using lock() or using Monitor object.
精彩评论