Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,30 @@ public static async Task BC_AddTakeCompleteAdding()
using (BlockingCollection<int> bc = new BlockingCollection<int>())
{
// Spin up a Task to populate the BlockingCollection
using (Task t1 = Task.Run(() =>
Task t1 = Task.Run(() =>
{
bc.Add(1);
bc.Add(2);
bc.Add(3);
bc.CompleteAdding();
}))
});

// Spin up a Task to consume the BlockingCollection
Task t2 = Task.Run(() =>
{
// Spin up a Task to consume the BlockingCollection
using (Task t2 = Task.Run(() =>
try
{
try
{
// Consume consume the BlockingCollection
while (true) Console.WriteLine(bc.Take());
}
catch (InvalidOperationException)
{
// An InvalidOperationException means that Take() was called on a completed collection
Console.WriteLine("That's All!");
}
}))
// Consume consume the BlockingCollection
while (true) Console.WriteLine(bc.Take());
}
catch (InvalidOperationException)
{
await Task.WhenAll(t1, t2);
// An InvalidOperationException means that Take() was called on a completed collection
Console.WriteLine("That's All!");
}
}
});

await Task.WhenAll(t1, t2);
}
}
}
Expand Down