当前位置: 首页>>代码示例>>C#>>正文


C# Task.Count方法代码示例

本文整理汇总了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);
 }
开发者ID:HannesR-O,项目名称:PSC2013,代码行数:13,代码来源:MatrixGenerator.cs

示例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();
                }
            }
        }
开发者ID:henricj,项目名称:AsyncWorkout,代码行数:18,代码来源:TaskWorkout.cs

示例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);
        }
开发者ID:kronic,项目名称:corefxlab,代码行数:51,代码来源:ChannelTestBase.cs

示例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;
        }
开发者ID:sethjuarez,项目名称:numl,代码行数:94,代码来源:MultiClassLearner.cs


注:本文中的Task.Count方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。