本文整理汇总了C#中System.Threading.Tasks.Task.ToArray方法的典型用法代码示例。如果您正苦于以下问题:C# Task.ToArray方法的具体用法?C# Task.ToArray怎么用?C# Task.ToArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.Tasks.Task
的用法示例。
在下文中一共展示了Task.ToArray方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ShouldSend100MessagesMultiThreadedWithTransactions
public void ShouldSend100MessagesMultiThreadedWithTransactions()
{
int count = 100;
var stop = Stopwatch.StartNew();
using (var provider = new XmsProducerProvider(true))
{
var taskFactory = new TaskFactory();
var tasks = new Task[count];
for (int i = 0; i < count; i++)
{
tasks[i] = taskFactory.StartNew(
() =>
{
using(var scope = new TransactionScope(TransactionScopeOption.Required))
{
provider.SendTestMessage(destination);
scope.Complete();
}
});
}
Task.WaitAll(tasks.ToArray());
stop.Stop();
}
Console.WriteLine("Sent {0} messages multi-threaded in {1}", count, stop.Elapsed);
destination.AssertMessageCount(count);
}
示例2: SendAsyncShouldHandleHighVolumeOfMessages
public async Task SendAsyncShouldHandleHighVolumeOfMessages(int amount, int maxAsync)
{
using (var router = new BrokerRouter(new KafkaOptions(IntegrationConfig.IntegrationUri)))
using (var producer = new Producer(router, maxAsync) { BatchSize = amount / 2 })
{
var tasks = new Task<ProduceResponse[]>[amount];
for (var i = 0; i < amount; i++)
{
tasks[i] = producer.SendMessageAsync(IntegrationConfig.IntegrationTopic,
new[] { new Message(Guid.NewGuid().ToString()) });
}
var results = await Task.WhenAll(tasks.ToArray());
//Because of how responses are batched up and sent to servers, we will usually get multiple responses per requested message batch
//So this assertion will never pass
//Assert.That(results.Count, Is.EqualTo(amount));
Assert.That(results.Any(x => x.Any(y => y.Error != 0)), Is.False,
"Should not have received any results as failures.");
}
}