本文整理汇总了C#中Task.Select方法的典型用法代码示例。如果您正苦于以下问题:C# Task.Select方法的具体用法?C# Task.Select怎么用?C# Task.Select使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Task
的用法示例。
在下文中一共展示了Task.Select方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
public void Run(int queries, int threads, int prepared)
{
Console.WriteLine("Preparing...");
_queries = queries;
_manager = new CartManager();
_times = new ConcurrentDictionary<string, LinkedList<long>>();
_stopwatch = new Stopwatch();
_count = 0;
_groups = Enumerable.Range(0, 9).Select(n => "group_" + n).ToArray();
//pregenerate carts
_manager.PrepareDbAsync().Wait();
var cartTasks = new Task<Cart>[prepared];
int groupId = 0;
for (int i = 0; i < prepared; i++)
{
cartTasks[i] = _manager.AddCartAsync(_groups[groupId]);
groupId = (groupId + 1) % _groups.Length;
}
Task.WaitAll(cartTasks);
var carts = cartTasks.Select(t => t.Result).ToArray();
Console.WriteLine("Executing...");
//execute!
_stopwatch.Start();
var doTasks = new LinkedList<Task>();
for (int i = 0; i < threads; i++)
{
doTasks.AddLast(DoAddCarts());
doTasks.AddLast(DoAddItems(carts));
//doTasks.AddLast(DoFindByGroupId());
doTasks.AddLast(DoGetItems(carts));
doTasks.AddLast(DoUpdateCarts(carts));
}
Task.WaitAll(doTasks.ToArray());
_stopwatch.Stop();
//print results
Console.WriteLine("Total tasks run: {0} in {1} ({2} req/s)", _count, _stopwatch.Elapsed,
DoubleString((double)queries / _stopwatch.ElapsedMilliseconds * 1000));
#if debug
Console.WriteLine(MemoryPool.Instance);
#endif
Console.WriteLine();
WriteRow("", "Calls", "Avg", "Median", "Min", "Max");
WriteStatistics("Total", _times.SelectMany(vls => vls.Value));
Console.WriteLine(new string('-', 60));
foreach (var call in _times)
{
WriteStatistics(call.Key, call.Value);
}
}
示例2: UpsertBlockOrSkipWithStressAsync
public void UpsertBlockOrSkipWithStressAsync()
{
BlobStorage.PutBlobAsync(ContainerName, BlobName, 0).Wait();
var array = new Task<BlobWithETag<int>>[8];
array = array
.AsParallel()
.Select(k => BlobStorage.UpsertBlobOrSkipAsync<int>(ContainerName, BlobName, () => 1, i => i + 1))
.ToArray();
Assert.IsFalse(array.Any(x => x.Result == null), "No skips");
var sorted = array.Select(m => m.Result.Blob)
.OrderBy(i => i)
.ToArray();
for (int i = 0; i < array.Length; i++)
{
Assert.AreEqual(i + 1, sorted[i], "Concurrency should be resolved, every call should increment by one.");
}
}
示例3: Search
/// <summary> A search implementation which executes each
/// <see cref="Searchable"/> in its own thread and waits for each search to complete
/// and merge the results back together.
/// </summary>
public override TopDocs Search(Weight weight, Filter filter, int nDocs)
{
HitQueue hq = new HitQueue(nDocs, false);
object lockObj = new object();
Task<TopDocs>[] tasks = new Task<TopDocs>[searchables.Length];
//search each searchable
for (int i = 0; i < searchables.Length; i++)
{
int cur = i;
tasks[i] =
Task.Factory.StartNew(() => MultiSearcherCallableNoSort(ThreadLock.MonitorLock, lockObj, searchables[cur], weight, filter,
nDocs, hq, cur, starts));
}
int totalHits = 0;
float maxScore = float.NegativeInfinity;
Task.WaitAll(tasks);
foreach(TopDocs topDocs in tasks.Select(x => x.Result))
{
totalHits += topDocs.TotalHits;
maxScore = Math.Max(maxScore, topDocs.MaxScore);
}
ScoreDoc[] scoreDocs = new ScoreDoc[hq.Size()];
for (int i = hq.Size() - 1; i >= 0; i--) // put docs in array
scoreDocs[i] = hq.Pop();
return new TopDocs(totalHits, scoreDocs, maxScore);
}
示例4: Multiplicate
public MatrixWrapper Multiplicate(int numberOfThreads)
{
_numberOfThreads = numberOfThreads;
if (_numberOfThreads > matrices.Count/2) throw new ApplicationException();
Task<MatrixWrapper>[] taskArray = new Task<MatrixWrapper>[numberOfThreads];
var num = matrices.Count/numberOfThreads; // 7/2 = 3
var lastAddition = matrices.Count - numberOfThreads*num; // 7 - 2*3 = 1
for (int i = 0; i < taskArray.Length; i++)
{
var range = matrices.GetRange(i*num, i == numberOfThreads - 1 ? num + lastAddition : num);
taskArray[i] = new Task<MatrixWrapper>(() => DoWork(range));
taskArray[i].Start();
}
Task.WaitAll(taskArray);
return taskArray.Length > 1 ? DoWork(taskArray.Select(t => t.Result).ToList()) : taskArray[0].Result;
}