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?
views:
30answers:
2
A:
Of course, you can, if you lock access to the queue using lock() or using Monitor object.
Eugene Mayevski 'EldoS Corp
2010-09-10 10:55:33
+3
A:
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.
Jon Skeet
2010-09-10 10:55:46
I cry for my 3.5 project :)
Tamás Szelei
2010-09-10 11:02:24
@sztomi: Creating a producer/consumer queue isn't *too* hard using locks and Wait/Notify. It just takes a certain amount of care :)
Jon Skeet
2010-09-10 11:16:50
Sure, I know that :). The question was about if I have to lock or not. But it's good to know they implemented that in 4, even if I can't use it right now, so thanks.
Tamás Szelei
2010-09-10 11:33:39