本文整理汇总了C#中Task.Count方法的典型用法代码示例。如果您正苦于以下问题:C# Task.Count方法的具体用法?C# Task.Count怎么用?C# Task.Count使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Task
的用法示例。
在下文中一共展示了Task.Count方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Wait
public void Wait()
{
Task[] tasks = new Task[2];
int i = 0;
if (_taskCells != null && _taskCells.Status == TaskStatus.Running)
tasks[i++] = _taskCells;
if (_taskHumans != null && _taskHumans.Status == TaskStatus.Running)
tasks[i++] = _taskHumans;
Task[] taskArr = new Task[tasks.Count(x => x != null)];
for (int j = 0; j < taskArr.Length; ++j)
taskArr[j] = tasks[j];
Task.WaitAll(taskArr);
}
示例2: ReadStrings
async Task<IEnumerable<string>> ReadStrings()
{
using (var httpClient = new HttpClient())
{
using (var registration = _token.Register(() => httpClient.CancelPendingRequests()))
{
var tasks = new Task<string>[100];
for (var i = 0; i < tasks.Count() && !_token.IsCancellationRequested; ++i)
tasks[i] = TaskEx.Run(() => ReadString(httpClient), _token);
var completed = await TaskEx.WhenAll(tasks).ConfigureAwait(false);
// Do a little CPU-bound work...
return completed.Distinct();
}
}
}
示例3: SelectAsync_CaseReadBeforeAvailable_Success
public async Task SelectAsync_CaseReadBeforeAvailable_Success()
{
if (RequiresSingleReaderWriter)
return;
IChannel<int> c1 = CreateChannel();
IChannel<int> c2 = CreateChannel();
IChannel<int> c3 = CreateChannel();
int total1 = 0, total2 = 0, total3 = 0;
int expectedTotal1 = 0, expectedTotal2 = 0, expectedTotal3 = 0;
var selects = new Task<bool>[12];
for (int i = 0; i < selects.Length; i++)
{
selects[i] = Channel
.CaseRead(c1, item => Interlocked.Add(ref total1, item))
.CaseRead(c2, item => { Interlocked.Add(ref total2, item); return Task.CompletedTask; })
.CaseRead(c3, async item => { await Task.Yield(); Interlocked.Add(ref total3, item); })
.SelectAsync();
}
var writes = new Task[selects.Length];
for (int i = 0; i < selects.Length; i++)
{
switch (i % 3)
{
case 0:
writes[i] = c1.WriteAsync(i);
expectedTotal1 += i;
break;
case 1:
writes[i] = c2.WriteAsync(i);
expectedTotal2 += i;
break;
case 2:
writes[i] = c3.WriteAsync(i);
expectedTotal3 += i;
break;
}
}
await Task.WhenAll(selects);
Assert.All(writes, write => Assert.Equal(TaskStatus.RanToCompletion, write.Status));
Assert.All(selects, select => Assert.Equal(TaskStatus.RanToCompletion, select.Status));
Assert.Equal(selects.Length, selects.Count(s => s.Result));
Assert.Equal(expectedTotal1, total1);
Assert.Equal(expectedTotal2, total2);
Assert.Equal(expectedTotal3, total3);
}
示例4: Learn
/// <summary>
/// Generate a multi-class classification model using a specialist classifier for each class label.
/// </summary>
/// <param name="generator">The generator to use for each individual classifier.</param>
/// <param name="examples">Training examples of any number of classes</param>
/// <param name="trainingPercentage">Percentage of training examples to use, i.e. 70% = 0.7</param>
/// <param name="mixingPercentage">Percentage to mix positive and negative exmaples, i.e. 50% will add an additional 50% of
/// <paramref name="trainingPercentage"/> of negative examples into each classifier when training.</param>
/// <param name="isMultiClass">Determines whether each class is mutually inclusive.
/// <para>For example: If True, each class takes on a number of classes and does not necessarily belong to one specific class.</para>
/// <para>The ouput would then be a number of predicted classes for a single prediction. E.g. A song would be True as it may belong to classes: vocals, rock as well as bass.</para>
/// </param>
/// <returns></returns>
public static ClassificationModel Learn(IGenerator generator, IEnumerable<object> examples, double trainingPercentage, double mixingPercentage = 0.5, bool isMultiClass = true)
{
Descriptor descriptor = generator.Descriptor;
trainingPercentage = (trainingPercentage > 1.0 ? trainingPercentage / 100 : trainingPercentage);
mixingPercentage = (mixingPercentage > 1.0 ? mixingPercentage / 100 : mixingPercentage);
var classGroups = examples.Select(s => new
{
Label = generator.Descriptor.GetValue(s, descriptor.Label),
Item = s
})
.GroupBy(g => g.Label)
.ToDictionary(k => k.Key, v => v.Select(s => s.Item).ToArray());
int classes = classGroups.Count();
Dictionary<object, IClassifier> models = null;
Score finalScore = new Score();
if (classes > 2)
{
models = new Dictionary<object, IClassifier>(classes);
Task<Tuple<IClassifier, Score, object>>[] learningTasks = new Task<Tuple<IClassifier, Score, object>>[classes];
for (int y = 0; y < classes; y++)
{
models.Add(classGroups.ElementAt(y).Key, null);
int mix = (int)System.Math.Ceiling(((classGroups.ElementAt(y).Value.Count() * trainingPercentage) * mixingPercentage) / classes);
object label = classGroups.ElementAt(y).Key;
object[] truthExamples = classGroups.ElementAt(y).Value;
object[] falseExamples = classGroups.Where(w => w.Key != classGroups.Keys.ElementAt(y))
.SelectMany(s => s.Value.Take(mix).ToArray())
.ToArray();
learningTasks[y] = Task.Factory.StartNew(
() => MultiClassLearner.GenerateModel(generator, truthExamples, falseExamples, label, trainingPercentage, label)
);
}
Task.WaitAll(learningTasks);
Score[] scores = new Score[learningTasks.Count()];
for (int c = 0; c < learningTasks.Count(); c++)
{
models[learningTasks[c].Result.Item3] = learningTasks[c].Result.Item1;
scores[c] = learningTasks[c].Result.Item2;
}
finalScore = Score.CombineScores(scores);
}
else
{
// fallback to single classifier for two class classification
var dataset = descriptor.Convert(examples, true).ToExamples();
var positives = examples.Slice(dataset.Item2.Indices(f => f == 1d)).ToArray();
var negatives = examples.Slice(dataset.Item2.Indices(w => w != 1d)).ToArray();
var label = generator.Descriptor.GetValue(positives.First(), descriptor.Label);
var model = MultiClassLearner.GenerateModel(generator, positives, negatives, label, trainingPercentage, label);
finalScore = model.Item2;
models = new Dictionary<object, IClassifier>() { { label, model.Item1 } };
}
ClassificationModel classificationModel = new ClassificationModel()
{
Generator = generator,
Classifiers = models,
IsMultiClass = isMultiClass,
Score = finalScore
};
return classificationModel;
}