本文整理汇总了C#中Task.Concat方法的典型用法代码示例。如果您正苦于以下问题:C# Task.Concat方法的具体用法?C# Task.Concat怎么用?C# Task.Concat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Task
的用法示例。
在下文中一共展示了Task.Concat方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Parallel_PutGetDelete_Random
public void Parallel_PutGetDelete_Random()
{
const int PUTTER_CNT = 2, PUTTER_OP_CNT = 2 * 10000;
const int GETTER_CNT = 6, GETTER_OP_CNT = 2 * 30000;
const int DELETER_CNT = 2, DELETER_OP_CNT = 2 * 10000;
var data = new ConcurrentDictionary<PilePointer, string>();
var getAccessViolations = new ConcurrentDictionary<int, int>();
var deleteAccessViolations = new ConcurrentDictionary<int, int>();
using (var pile = new DefaultPile())
{
pile.Start();
var ipile = pile as IPile;
// putter tasks
var putters = new Task[PUTTER_CNT];
for (int it = 0; it < PUTTER_CNT; it++)
{
var task = new Task(() => {
for (int i = 0; i < PUTTER_OP_CNT; i++)
{
var str = NFX.Parsing.NaturalTextGenerator.Generate();
var pp = ipile.Put(str);
data.TryAdd(pp, str);
}
});
putters[it] = task;
}
// getter tasks
var getters = new Task[GETTER_CNT];
for (int it = 0; it < GETTER_CNT; it++)
{
var task = new Task(() => {
for (int i = 0; i < GETTER_OP_CNT; i++)
{
if (data.Count == 0) {
System.Threading.Thread.Yield();
continue;
}
var idx = ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, data.Count-1);
var kvp = data.ElementAt(idx);
try
{
var str = ipile.Get(kvp.Key);
Assert.AreEqual(str, kvp.Value);
}
catch (PileAccessViolationException)
{
getAccessViolations.AddOrUpdate(System.Threading.Thread.CurrentThread.ManagedThreadId, 1, (mid, val) => val + 1);
}
}
});
getters[it] = task;
}
// deleter tasks
var deleters = new Task[DELETER_CNT];
for (int it = 0; it < DELETER_CNT; it++)
{
var task = new Task(() => {
for (int i = 0; i < DELETER_OP_CNT; i++)
{
if (data.Count == 0) {
System.Threading.Thread.Yield();
continue;
}
var idx = ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, data.Count-1);
var kvp = data.ElementAt(idx);
try
{
ipile.Delete(kvp.Key);
}
catch (PileAccessViolationException)
{
deleteAccessViolations.AddOrUpdate(System.Threading.Thread.CurrentThread.ManagedThreadId, 1, (mid, val) => val + 1);
}
}
});
deleters[it] = task;
}
foreach (var task in putters) task.Start();
foreach (var task in getters) task.Start();
foreach (var task in deleters) task.Start();
Task.WaitAll(putters.Concat(getters).Concat(deleters).ToArray());
foreach (var kvp in getAccessViolations)
//.........这里部分代码省略.........
示例2: ProcessCompilationEventsAsync
private async Task ProcessCompilationEventsAsync(AnalysisScope analysisScope, AnalysisState analysisStateOpt, bool prePopulatedEventQueue, CancellationToken cancellationToken)
{
try
{
CompilationCompletedEvent completedEvent = null;
if (analysisScope.ConcurrentAnalysis)
{
// Kick off worker tasks to process all compilation events (except the compilation end event) in parallel.
// Compilation end event must be processed after all other events.
var workerCount = prePopulatedEventQueue ? Math.Min(CompilationEventQueue.Count, _workerCount) : _workerCount;
var workerTasks = new Task<CompilationCompletedEvent>[workerCount];
for (int i = 0; i < workerCount; i++)
{
workerTasks[i] = ProcessCompilationEventsCoreAsync(analysisScope, analysisStateOpt, prePopulatedEventQueue, cancellationToken);
}
cancellationToken.ThrowIfCancellationRequested();
// Kick off tasks to execute syntax tree actions.
var syntaxTreeActionsTask = Task.Run(() => ExecuteSyntaxTreeActions(analysisScope, analysisStateOpt, cancellationToken));
// Wait for all worker threads to complete processing events.
await Task.WhenAll(workerTasks.Concat(syntaxTreeActionsTask)).ConfigureAwait(false);
for (int i = 0; i < workerCount; i++)
{
if (workerTasks[i].Status == TaskStatus.RanToCompletion && workerTasks[i].Result != null)
{
completedEvent = workerTasks[i].Result;
break;
}
}
}
else
{
completedEvent = await ProcessCompilationEventsCoreAsync(analysisScope, analysisStateOpt, prePopulatedEventQueue, cancellationToken).ConfigureAwait(false);
ExecuteSyntaxTreeActions(analysisScope, analysisStateOpt, cancellationToken);
}
// Finally process the compilation completed event, if any.
if (completedEvent != null)
{
ProcessEvent(completedEvent, analysisScope, analysisStateOpt, cancellationToken);
}
}
catch (Exception e) when (FatalError.ReportUnlessCanceled(e))
{
throw ExceptionUtilities.Unreachable;
}
}
示例3: ProcessCompilationEventsAsync
private async Task ProcessCompilationEventsAsync(CancellationToken cancellationToken)
{
CompilationCompletedEvent completedEvent = null;
// Kick off worker tasks to process all compilation events (except the compilation end event) in parallel.
// Compilation end event must be processed after all other events.
var workerTasks = new Task[_workerCount];
for (int i = 0; i < _workerCount; i++)
{
workerTasks[i] = Task.Run(async () =>
{
var result = await ProcessCompilationEventsCoreAsync(cancellationToken).ConfigureAwait(false);
if (result != null)
{
completedEvent = result;
}
}, cancellationToken);
}
// Kick off tasks to execute syntax tree actions.
var syntaxTreeActionsTask = ExecuteSyntaxTreeActions(cancellationToken);
// Wait for all worker threads to complete processing events.
await Task.WhenAll(workerTasks.Concat(syntaxTreeActionsTask)).ConfigureAwait(false);
// Finally process the compilation completed event, if any.
if (completedEvent != null)
{
await ProcessEventAsync(completedEvent, cancellationToken).ConfigureAwait(false);
}
}