Skip to content

Instantly share code, notes, and snippets.

@naraga
Created September 18, 2012 22:37
Show Gist options
  • Save naraga/3746433 to your computer and use it in GitHub Desktop.
Save naraga/3746433 to your computer and use it in GitHub Desktop.
BlockingCollection demo
class Program
{
// BlockingCollection - demo (approaching Q3)
static void Main(string[] args)
{
var coll = new BlockingCollection<string>();
Task.Run(() =>
{
Thread.Sleep(1000);
coll.TryAdd(DateTime.Now.ToString(CultureInfo.InvariantCulture));
}).ContinueWith(task =>
{
Thread.Sleep(1000);
coll.TryAdd(DateTime.Now.ToString(CultureInfo.InvariantCulture));
}).ContinueWith(task =>
{
coll.CompleteAdding();
});
foreach (var item in coll.GetConsumingEnumerable())
{
Console.WriteLine(item);
}
Console.WriteLine("press any key");
Console.ReadLine();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment