private static async Task SaveData(ParsedArguments args, CancellationToken cancellationToken, ValueProcessor a, int[] array, bool hasValuesToSave, int max, int min, long sum) { if (!Directory.Exists(args.OutputDirectory)) { Directory.CreateDirectory(args.OutputDirectory); } await a.SaveAsync(args.OutputDirectory, array, cancellationToken); if (hasValuesToSave) { var id = DateTime.Now.Ticks.ToString(); if (args.MaxValue) { await FileUtilities.SaveValue(args.OutputDirectory, "result_max_"+ id, max); } if (args.MinValue) { await FileUtilities.SaveValue(args.OutputDirectory, "result_min_" + id, min); } if (args.SumValues) { await FileUtilities.SaveValue(args.OutputDirectory, "result_sum_" + id, sum); } } }
private static async Task DoCalculations(ParsedArguments args, CancellationToken cancellationToken) { var a = new ValueProcessor(); var array = await a.CreateAsync(args.Count, cancellationToken); var min = int.MaxValue; var max = int.MinValue; var sum = 0; var math = new SlowMath(); bool hasValuesToSave = false; var tasks = new List<Task>(); LogValues(array); if (args.SumValues) { var task = math.SumAsync(array, cancellationToken).ContinueWith(x => { sum = x.Result; Console.WriteLine("SUM: {0}", sum); Log.Trace("SUM: " + sum); hasValuesToSave = true; }, TaskContinuationOptions.OnlyOnRanToCompletion); tasks.Add(task); } if (args.MaxValue) { var task = math.MinAsync(array, cancellationToken).ContinueWith(x => { max = x.Result; Console.WriteLine("MAX: {0}", max); Log.Trace("MAX: "+max); hasValuesToSave = true; }, TaskContinuationOptions.OnlyOnRanToCompletion); tasks.Add(task); } if (args.MinValue) { var task = math.MinAsync(array, cancellationToken).ContinueWith(x => { min = x.Result; Console.WriteLine("MIN: {0}", min); Log.Trace("MIN: " + min); hasValuesToSave = true; }, TaskContinuationOptions.OnlyOnRanToCompletion); tasks.Add(task); } Task.WaitAll(tasks.ToArray()); if (args.OutputDirectory != null) { await SaveData(args, cancellationToken, a, array, hasValuesToSave, max, min, sum); } }