本文整理汇总了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;
}
示例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);
}
示例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;
}
示例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);
}
示例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);
}