I want two threads on the same machine, one that populates a collection and the other one that pops data out of it as soon as it's available and stops when it know it's over. I just don't know what collection to use...
private void DataProviderThread()
{
GlobalCollection = new SomeMagicCollection();
for (int i = 0; i < 100; i++)
{
GlobalCollection.Add(new SomeDataItem(i));
Thread.Sleep(100);
}
GlobalCollection.IHaveFinishedPopulatingThanksAndBye();
}
private void DataCruncherThread()
{
foreach (var item in GlobalCollection)
{
// Do whatever
}
// The control should exit foreach only once the data provider states that the collection is finished
}
I then want to iterate simply on it, having the Collection take care of
- Staying thread safe
- Grant the standard IEnumerable functionalities
- Let my data cruncher thread wait for new items until the DataBuilder has explicitely called
IHaveFinishedPopulatingThanksAndBye()
, then cleanly exit the loop - Allow me to have several other threads iterating with the same constraints
I can't believe C# doesn't ship this in a new version. But what's its name?