Beispiel #1
0
        private static void DoMainWork(ParsedArguments parsedArgs, CancellationTokenSource token)
        {
            var progressChars = new[] { '-', '\\', '|', '/' };

            var watch = new Stopwatch();
            watch.Start();
            var calcTask = DoCalculations(parsedArgs, token.Token);
            try
            {
                WaitForTaskCompletion(calcTask, progressChars);

                watch.Stop();

                Log.Trace("Task was completed in " + watch.Elapsed);
                Console.WriteLine("Task was completed in " + watch.Elapsed);
            }
            catch (AggregateException ex)
            {
                HandleException(ex);
            }
        }
Beispiel #2
0
        private static void Main(string[] args)
        {
            Log.Trace("Application started");
            Console.WriteLine("Test task for Brokeree");
            Console.WriteLine();

            var parsedArgs = new ParsedArguments(args);
            if (parsedArgs.Count == -1)
            {
                Console.WriteLine(parsedArgs.ErrorMessage);
                return;
            }
            WriteArguments(parsedArgs);

            var token = new CancellationTokenSource();
            CancellationTokenSource keyHandlerCancellationToken;
            var keyHandlerTask = InitKeyboardHandler(token, out keyHandlerCancellationToken);

            DoMainWork(parsedArgs, token);

            keyHandlerCancellationToken.Cancel();
            Console.WriteLine("Exiting...");
        }
Beispiel #3
0
        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);
            }
        }
Beispiel #4
0
        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);
                }
            }
        }
Beispiel #5
0
 private static void WriteArguments(ParsedArguments parsedArgs)
 {
     Console.WriteLine("Count: {0}", parsedArgs.Count);
     Console.WriteLine("Sum: {0}", parsedArgs.SumValues);
     Console.WriteLine("Max: {0}", parsedArgs.MaxValue);
     Console.WriteLine("Min: {0}", parsedArgs.MinValue);
     Console.WriteLine("Dir: {0}", parsedArgs.OutputDirectory);
     Console.WriteLine();
 }