Skip to content

Commit 866dbb2

Browse files
authored
Remove usings in first BlockingCollection example (#4264)
using statement not needed with Task when using Task.WhenAll or Task.WaitAll after .NET 4.5
1 parent e729e37 commit 866dbb2

File tree

1 file changed

+15
-17
lines changed
  • samples/snippets/csharp/VS_Snippets_CLR_System/system.collections.concurrent.blockingcollection/cs

1 file changed

+15
-17
lines changed

samples/snippets/csharp/VS_Snippets_CLR_System/system.collections.concurrent.blockingcollection/cs/blockingcoll.cs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,30 @@ public static async Task BC_AddTakeCompleteAdding()
2727
using (BlockingCollection<int> bc = new BlockingCollection<int>())
2828
{
2929
// Spin up a Task to populate the BlockingCollection
30-
using (Task t1 = Task.Run(() =>
30+
Task t1 = Task.Run(() =>
3131
{
3232
bc.Add(1);
3333
bc.Add(2);
3434
bc.Add(3);
3535
bc.CompleteAdding();
36-
}))
36+
});
37+
38+
// Spin up a Task to consume the BlockingCollection
39+
Task t2 = Task.Run(() =>
3740
{
38-
// Spin up a Task to consume the BlockingCollection
39-
using (Task t2 = Task.Run(() =>
41+
try
4042
{
41-
try
42-
{
43-
// Consume consume the BlockingCollection
44-
while (true) Console.WriteLine(bc.Take());
45-
}
46-
catch (InvalidOperationException)
47-
{
48-
// An InvalidOperationException means that Take() was called on a completed collection
49-
Console.WriteLine("That's All!");
50-
}
51-
}))
43+
// Consume consume the BlockingCollection
44+
while (true) Console.WriteLine(bc.Take());
45+
}
46+
catch (InvalidOperationException)
5247
{
53-
await Task.WhenAll(t1, t2);
48+
// An InvalidOperationException means that Take() was called on a completed collection
49+
Console.WriteLine("That's All!");
5450
}
55-
}
51+
});
52+
53+
await Task.WhenAll(t1, t2);
5654
}
5755
}
5856
}

0 commit comments

Comments
 (0)