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


C# Task.Sum方法代码示例

本文整理汇总了C#中System.Threading.Tasks.Task.Sum方法的典型用法代码示例。如果您正苦于以下问题:C# Task.Sum方法的具体用法?C# Task.Sum怎么用?C# Task.Sum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Threading.Tasks.Task的用法示例。


在下文中一共展示了Task.Sum方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetAsyncCallbackData

 public int GetAsyncCallbackData(int value, int asyncCallbacksToMake)
 {
     int result = value;
     var tasks = new Task<int>[asyncCallbacksToMake];
     for (int i = 0; i < asyncCallbacksToMake; i++)
     {
         tasks[i] = OperationContext.Current.GetCallbackChannel<IDuplexCallback>().EchoGetAsyncCallbackData(result + i);
     }
     Task.WhenAll(tasks).Wait();
     result = tasks.Sum((t) => t.Result);
     return result;
 }
开发者ID:weshaggard,项目名称:wcf,代码行数:12,代码来源:DuplexService.svc.cs

示例2: Sum

        public static float Sum(float[] values, Func<float, float> selector, int threadsCount)
        {
            if (threadsCount < 2 || values.Length <= threadsCount)
                return selector == null ? values.Sum() : values.Sum(selector);

            Task<float>[] tasks = new Task<float>[threadsCount];
            int chunk = (int)Math.Ceiling((double)values.Length/threadsCount);
            for (int i = 0; i < threadsCount; i++)
            {
                int start = i*chunk;
                int end = Math.Min(start + chunk, values.Length);
                tasks[i] = Task.Run(() => Sum(values, selector, start, end));
            }

            return tasks.Sum(t => t.Result);
        }
开发者ID:es-repo,项目名称:wlpgnr,代码行数:16,代码来源:MathUtilities.cs

示例3: RunConcurrentPerformanceTest

        public static PerformanceResult RunConcurrentPerformanceTest(int numIterations, int degreeParallelism,
            Action operation)
        {
            int i;
            var taskList = new Task<PerformanceResult>[degreeParallelism];
            long startTime = HiResTimer.Ticks;
            int subIterations = numIterations/degreeParallelism;

            for (i = 0; i < degreeParallelism; i++)
            {
                var t = new Task<PerformanceResult>(() => RunPerformanceTest(subIterations, operation, true));
                taskList[i] = t;
            }

            for (i = 0; i < degreeParallelism; i++) taskList[i].Start();

            Task.WaitAll(taskList);
            long stopTime = HiResTimer.Ticks;

            var rawData = new List<double>();

            for (i = 0; i < degreeParallelism; i++)
            {
                rawData.AddRange(taskList[i].Result.DescriptiveResult.RawData);
            }

            var desc = new DescriptiveAnalysis(rawData);
            desc.Analyze(false);
            desc.AnalyzeHistogram(cHistogramBuckets);

            var res = new PerformanceResult
            {
                IsValid = true,
                Iterations = taskList.Sum(p => p.Result.Iterations),
                DegreeOfParallelism = degreeParallelism,
                TotalMilliseconds = ConvertToMs(startTime, stopTime),
                TotalSeconds = ConvertToSeconds(startTime, stopTime),
                TotalTicks = stopTime - startTime,
                DescriptiveResult = desc.Result
            };

            for (i = 0; i < degreeParallelism; i++) taskList[i].Dispose();

            return res;
        }
开发者ID:hypertheory-training,项目名称:PerformanceUtilities,代码行数:45,代码来源:PerformancePatterns.cs

示例4: DocFreq

	    /// <summary>
	    /// Executes each <see cref="Searchable"/>'s docFreq() in its own thread and 
	    /// waits for each search to complete and merge the results back together.
	    /// </summary>
		public override int DocFreq(Term term)
	    {
	        Task<int>[] tasks = new Task<int>[searchables.Length];
            for (int i = 0; i < searchables.Length; i++)
            {
                Searchable searchable = searchables[i];
                tasks[i] = Task.Factory.StartNew(() => searchable.DocFreq(term));
            }

	        Task.WaitAll(tasks);
	        return tasks.Sum(task => task.Result);
	    }
开发者ID:mindis,项目名称:Transformalize,代码行数:16,代码来源:ParallelMultiSearcher.cs

示例5: CalculateIntegral

        private static double CalculateIntegral(Calculator calc, Func<Calculator, double> distanceMethod, double leftBound, double rightBound)
        {
            // Protip: the Wolfram Alpha way of verifying the results from this is:
            // "integral of (<<top>> - <<bottom>>)dx between <<start>> and <<stop>>"

            int numCores = Environment.ProcessorCount;

            int iterCount = (int)Math.Ceiling((rightBound - leftBound) / Increment);
            int itersPerCore = (int)Math.Floor((double)iterCount / numCores);

            // The number of iterations that the last thread should do (to accomodate for rounding errors)
            int lastCoreIters = itersPerCore + (iterCount % numCores);

            // Assemble the worker threads
            Task<double>[] tasks = new Task<double>[numCores];
            int core = 0;

            bool lastTask = false;
            while (!lastTask)
            {
                lastTask = core + 1 >= numCores;

                double start = leftBound + ((core * itersPerCore) * Increment);
                int numIters = (lastTask ? lastCoreIters : itersPerCore);

                tasks[core++] = new Task<double>(() => CalculateAreaChunk(new Calculator(calc), distanceMethod, start, rightBound, numIters));
            }

            foreach (Task<double> t in tasks)
            {
                t.Start();
            }

            Task.WaitAll(tasks);

            double totalArea = tasks.Sum(t => t.Result);

            // Find the number of decimal places the increment goes to
            int decPlace = 0;
            double slicedIncrement = Increment;

            // Move the decimal point of the increment to the right on each iteration. We keep
            // moving the decimal over until the first significant digit of the increment is found
            while ((int)(slicedIncrement *= 10) == 0)
            {
                decPlace++;
            }

            // Assume the area is only accurate to the number of decimal places that the increment has
            return Math.Round(totalArea, decPlace + 1);
        }
开发者ID:mgbowen,项目名称:graphing-calc-csharp,代码行数:51,代码来源:IntegralCalculator.cs


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