views:

30

answers:

2

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?

A: 

Of course, you can, if you lock access to the queue using lock() or using Monitor object.

Eugene Mayevski 'EldoS Corp
+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
I cry for my 3.5 project :)
Tamás Szelei
@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
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