本文整理汇总了C#中BlockingCollection.ToArray方法的典型用法代码示例。如果您正苦于以下问题:C# BlockingCollection.ToArray方法的具体用法?C# BlockingCollection.ToArray怎么用?C# BlockingCollection.ToArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BlockingCollection
的用法示例。
在下文中一共展示了BlockingCollection.ToArray方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateNodeSet
public NodeSet CreateNodeSet(int size)
{
if(size > 1000)
throw new ArgumentOutOfRangeException("size", "size should be less than 1000");
TraceCorrelation trace = new TraceCorrelation(NodeServerTrace.Trace, "Creating node set of size " + size);
NodeSet set = new NodeSet();
using(trace.Open())
{
while(set.Count() < size)
{
var peerToGet = size - set.Count();
var activePeers = PeerTable.GetActivePeers(1000);
activePeers = activePeers.Where(p => !set.Contains(p.NetworkAddress.Endpoint)).ToArray();
if(activePeers.Length < peerToGet)
{
DiscoverPeers(size);
continue;
}
NodeServerTrace.Information("Need " + peerToGet + " more nodes");
BlockingCollection<Node> handshakedNodes = new BlockingCollection<Node>(peerToGet);
CancellationTokenSource handshakedFull = new CancellationTokenSource();
try
{
Parallel.ForEach(activePeers,
new ParallelOptions()
{
MaxDegreeOfParallelism = 10,
CancellationToken = handshakedFull.Token
}, p =>
{
if(set.Contains(p.NetworkAddress.Endpoint))
return;
Node node = null;
try
{
node = GetNodeByPeer(p, handshakedFull.Token);
node.VersionHandshake(handshakedFull.Token);
if(node != null && node.State != NodeState.HandShaked)
node.Disconnect();
if(!handshakedNodes.TryAdd(node))
{
handshakedFull.Cancel();
node.Disconnect();
}
else
{
var remaining = (size - set.Count() - handshakedNodes.Count);
if(remaining == 0)
{
handshakedFull.Cancel();
}
else
NodeServerTrace.Information("Need " + remaining + " more nodes");
}
}
catch(Exception)
{
if(node != null)
node.Disconnect();
}
});
}
catch(OperationCanceledException)
{
}
set.AddNodes(handshakedNodes.ToArray());
}
}
return set;
}