本文整理汇总了C#中ConcurrentSet.AddOrThrow方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentSet.AddOrThrow方法的具体用法?C# ConcurrentSet.AddOrThrow怎么用?C# ConcurrentSet.AddOrThrow使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentSet
的用法示例。
在下文中一共展示了ConcurrentSet.AddOrThrow方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RunAsync
public async Task RunAsync() {
Console.WriteLine(DateTime.Now);
const int kMessagesPerWorker = 200000;
var sink = courierFacades[0];
var senders = courierFacades.Skip(1).ToArray();
var counter = kMessagesPerWorker * senders.Length;
var doneSignal = new AsyncLatch();
int upCounter = 0;
var set = new ConcurrentSet<int>();
sink.InboundMessageRouter.RegisterHandler<string>(
x => {
set.AddOrThrow(int.Parse(x.Body));
var newCounter = Interlocked.Decrement(ref counter);
Interlocked.Increment(ref upCounter);
if (upCounter % 500 == 0)
Console.WriteLine(newCounter + " " + upCounter);
if (newCounter == 0) {
doneSignal.Set();
}
return Task.FromResult(false);
});
var sync = new AsyncCountdownLatch(senders.Length);
var senderTasks = senders.Select((s, id) => Go(async () => {
await s.PeerTable.GetOrAdd(sink.Identity.Id).WaitForDiscoveryAsync().ConfigureAwait(false);
sync.Signal();
await sync.WaitAsync().ConfigureAwait(false);
Console.WriteLine("Sink discovered: " + DateTime.Now);
const int kBatchFactor = 1;
for (var batch = 0; batch < kBatchFactor; batch++) {
var batchSize = kMessagesPerWorker / kBatchFactor;
await Task.WhenAll(Util.Generate(
batchSize,
i => s.Messenger.SendReliableAsync(
"" + (batch * batchSize + i + id * kMessagesPerWorker),
sink.Identity.Id))
).ConfigureAwait(false);
}
Console.WriteLine("Worker Done: " + DateTime.Now);
}));
await Task.WhenAll(senderTasks).ConfigureAwait(false);
Console.WriteLine("Senders Done: " + DateTime.Now);
await doneSignal.WaitAsync().ConfigureAwait(false);
Console.WriteLine("Done Signalled: " + DateTime.Now);
AssertCollectionDeepEquals(set, new ConcurrentSet<int>(Enumerable.Range(0, kMessagesPerWorker * senders.Length)));
while (true) {
GC.Collect();
}
}