本文整理汇总了C#中Task.ToArray方法的典型用法代码示例。如果您正苦于以下问题:C# Task.ToArray方法的具体用法?C# Task.ToArray怎么用?C# Task.ToArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Task
的用法示例。
在下文中一共展示了Task.ToArray方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ShouldRead100MessagesMultiThreaded
public void ShouldRead100MessagesMultiThreaded()
{
int expected = 100;
destination.FillWith(expected);
var stop = Stopwatch.StartNew();
using (var provider = new XmsConsumerProvider(false))
{
var taskFactory = new TaskFactory();
var tasks = new Task[expected];
for (int i = 0; i < expected; i++)
{
tasks[i] = taskFactory.StartNew(
() =>
{
using(var consumer = provider.GetConsumer(destination))
{
var message = consumer.ReceiveNoWait();
if (message != null) Interlocked.Increment(ref actual);
}
});
}
Task.WaitAll(tasks.ToArray());
stop.Stop();
}
Console.WriteLine("Received {0} messages multi-threaded in {1}", expected, stop.Elapsed);
Assert.That(actual, Is.EqualTo(expected));
}
示例2: ConnectWorker
private void ConnectWorker()
{
// get peer counts
var connectedCount = this.connectedPeers.Count;
var pendingCount = this.pendingPeers.Count;
var unconnectedCount = this.unconnectedPeers.Count;
var maxConnections = Math.Max(CONNECTED_MAX + 20, PENDING_MAX);
// if there aren't enough peers connected and there is a pending connection slot available, make another connection
if (connectedCount < CONNECTED_MAX
&& pendingCount < PENDING_MAX
&& (connectedCount + pendingCount) < maxConnections
&& unconnectedCount > 0)
{
// grab a snapshot of unconnected peers
var unconnectedPeersLocal = this.unconnectedPeers.SafeToList();
// get number of connections to attempt
var connectCount = Math.Min(unconnectedCount, maxConnections - (connectedCount + pendingCount));
var connectTasks = new Task[connectCount];
for (var i = 0; i < connectCount; i++)
{
// cooperative loop
this.shutdownToken.Token.ThrowIfCancellationRequested();
// get a random peer to connect to
var remoteEndpoint = unconnectedPeersLocal.RandomOrDefault();
connectTasks[i] = ConnectToPeer(remoteEndpoint);
}
// wait for pending connection attempts to complete
Task.WaitAll(connectTasks.ToArray());
}
// check if there are too many peers connected
var overConnected = this.connectedPeers.Count - CONNECTED_MAX;
if (overConnected > 0)
{
foreach (var remoteEndpoint in this.connectedPeers.Keys.Take(overConnected))
{
// cooperative loop
this.shutdownToken.Token.ThrowIfCancellationRequested();
//Debug.WriteLine(string.Format("Too many peers connected ({0}), disconnecting {1}", overConnected, remoteEndpoint));
DisconnectPeer(remoteEndpoint, null);
}
}
}
示例3: StopEvent
public static void StopEvent(AMSQueueItem message, CancellationToken cancellationToken)
{
AMSEvent eventData = null;
try
{
eventData = AMSEventSqlAdapter.Instance.LoadByID(message.ResourceID);
if (eventData != null)
{
if (eventData.State == AMSEventState.Running)
AMSEventSqlAdapter.Instance.UpdateState(eventData.ID, AMSEventState.Stopping);
AMSEventChannelCollection ecs = AMSEventSqlAdapter.Instance.LoadEventAndChannel(eventData.ID);
Task[] stopProgramTasks = new Task[ecs.Count];
for (int i = 0; i < stopProgramTasks.Length; i++)
{
AMSEventChannel ec = ecs[i];
stopProgramTasks[i] = Task.Factory.StartNew(() => StopOneProgram(eventData, ec, cancellationToken));
}
Task.WaitAll(stopProgramTasks.ToArray());
AMSEventSqlAdapter.Instance.UpdateCompletedStateByChannels(eventData.ID);
}
}
finally
{
if (eventData != null)
LockHelper.Unlock(eventData);
}
}