本文整理汇总了C#中ConcurrentQueue.TryTake方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentQueue.TryTake方法的具体用法?C# ConcurrentQueue.TryTake怎么用?C# ConcurrentQueue.TryTake使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentQueue
的用法示例。
在下文中一共展示了ConcurrentQueue.TryTake方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IProducerConsumerCollection_TryAddTryTake_Success
public void IProducerConsumerCollection_TryAddTryTake_Success()
{
IProducerConsumerCollection<int> pcc = new ConcurrentQueue<int>();
Assert.True(pcc.TryAdd(1));
Assert.True(pcc.TryAdd(2));
int item;
Assert.True(pcc.TryTake(out item));
Assert.Equal(1, item);
Assert.True(pcc.TryTake(out item));
Assert.Equal(2, item);
}
示例2: DemoInterface
private static void DemoInterface()
{
Console.WriteLine("Demo Concurrent Interface ----------------------");
// can change this to concurrent stack or bag
IProducerConsumerCollection<string> shirts = new ConcurrentQueue<string>();
shirts.TryAdd("Pluralsight");
shirts.TryAdd("WordPress");
shirts.TryAdd("Code School");
Console.WriteLine("After enqueuing, count = " + shirts.Count);
string item1; // = shirts.Dequeue();
bool success = shirts.TryTake(out item1);
if (success)
Console.WriteLine("\r\nRemoving " + item1);
else
Console.WriteLine("queue was empty");
Console.WriteLine("\r\nEnumerating:");
foreach (string item in shirts)
Console.WriteLine(item);
Console.WriteLine("\r\nAfter enumerating, count = " + shirts.Count);
}